From 0aeb9912e0ced5ba0f75986dfe6e4b1a5e1bbe44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Mon, 9 Dec 2024 12:45:58 +0100 Subject: [PATCH] Now the database functionality works again --- .../AddChannelPricingIndicesSubscriber.php | 46 ----------- .../AddTimestampableIndicesSubscriber.php | 78 ------------------ src/Model/ChannelPricingInterface.php | 21 +---- src/Model/ChannelPricingTrait.php | 81 +------------------ src/Resources/config/services.xml | 1 - .../config/services/event_listener.xml | 22 ----- tests/Application/.env | 4 +- .../{Entity => Model}/ChannelPricing.php | 2 +- .../Application/config/packages/_sylius.yaml | 7 ++ .../Application/config/packages/doctrine.yaml | 8 +- 10 files changed, 16 insertions(+), 254 deletions(-) delete mode 100644 src/EventListener/AddChannelPricingIndicesSubscriber.php delete mode 100644 src/EventListener/AddTimestampableIndicesSubscriber.php delete mode 100644 src/Resources/config/services/event_listener.xml rename tests/Application/{Entity => Model}/ChannelPricing.php (98%) diff --git a/src/EventListener/AddChannelPricingIndicesSubscriber.php b/src/EventListener/AddChannelPricingIndicesSubscriber.php deleted file mode 100644 index 388c874..0000000 --- a/src/EventListener/AddChannelPricingIndicesSubscriber.php +++ /dev/null @@ -1,46 +0,0 @@ -getClassMetadata(); - - if (!is_subclass_of($metadata->name, ChannelPricingInterface::class, true)) { - return; - } - - if (!$metadata->hasField('multiplier')) { - throw new RuntimeException(sprintf('No "multiplier" property on class %s', $metadata->name)); - } - - $columnName = $metadata->getColumnName('multiplier'); - - /** @psalm-suppress PropertyTypeCoercion */ - $metadata->table = array_merge_recursive([ - 'indexes' => [ - [ - 'columns' => [$columnName], - ], - ], - ], $metadata->table); - } -} diff --git a/src/EventListener/AddTimestampableIndicesSubscriber.php b/src/EventListener/AddTimestampableIndicesSubscriber.php deleted file mode 100644 index f2faa50..0000000 --- a/src/EventListener/AddTimestampableIndicesSubscriber.php +++ /dev/null @@ -1,78 +0,0 @@ - */ - private array $classes; - - /** - * @param array $classes - */ - public function __construct(array $classes) - { - $this->classes = $classes; - } - - public function getSubscribedEvents(): array - { - return [ - Events::loadClassMetadata, - ]; - } - - public function loadClassMetadata(LoadClassMetadataEventArgs $event): void - { - $metadata = $event->getClassMetadata(); - - foreach ($this->classes as $class) { - if (!is_a($metadata->name, $class, true)) { - continue; - } - - self::addIndices($metadata); - } - } - - private static function addIndices(ClassMetadata $metadata): void - { - $indices = []; - - if (!is_subclass_of($metadata->name, TimestampableInterface::class, true)) { - throw new RuntimeException(sprintf( - 'The class %s must implement the interface, %s', - $metadata->name, - TimestampableInterface::class, - )); - } - - $fields = ['createdAt', 'updatedAt']; - foreach ($fields as $field) { - if (!$metadata->hasField($field)) { - throw new RuntimeException(sprintf('The class %s does not have a "%s" field', $metadata->name, $field)); - } - - $column = $metadata->getColumnName($field); - - $indices[] = [ - 'columns' => [$column], - ]; - } - - /** @psalm-suppress PropertyTypeCoercion */ - $metadata->table = array_merge_recursive([ - 'indexes' => $indices, - ], $metadata->table); - } -} diff --git a/src/Model/ChannelPricingInterface.php b/src/Model/ChannelPricingInterface.php index cc084e7..bae558c 100644 --- a/src/Model/ChannelPricingInterface.php +++ b/src/Model/ChannelPricingInterface.php @@ -5,32 +5,13 @@ namespace Setono\SyliusCatalogPromotionPlugin\Model; use Sylius\Component\Core\Model\ChannelPricingInterface as BaseChannelPricingInterface; -use Sylius\Component\Resource\Model\TimestampableInterface; -interface ChannelPricingInterface extends BaseChannelPricingInterface, TimestampableInterface +interface ChannelPricingInterface extends BaseChannelPricingInterface { - public function hasDiscount(): bool; - - public function getDiscountAmount(): ?int; - - /** - * If $asInteger is true it returns the discount rounded to the nearest whole number - */ - public function getDisplayableDiscount(bool $asInteger = false): ?float; - - /** - * @return bool Returns true if this was discounted manually - */ public function isManuallyDiscounted(): bool; /** * todo we need to update this via the resource controller events */ public function setManuallyDiscounted(bool $manuallyDiscounted): void; - - public function getMultiplier(): float; - - public function getBulkIdentifier(): ?string; - - public function resetBulkIdentifier(): void; } diff --git a/src/Model/ChannelPricingTrait.php b/src/Model/ChannelPricingTrait.php index d50fb60..5cb0923 100644 --- a/src/Model/ChannelPricingTrait.php +++ b/src/Model/ChannelPricingTrait.php @@ -4,78 +4,18 @@ namespace Setono\SyliusCatalogPromotionPlugin\Model; -use DateTimeInterface; use Doctrine\ORM\Mapping as ORM; -use Gedmo\Mapping\Annotation as Gedmo; use Sylius\Component\Core\Model\ChannelPricing; use Sylius\Component\Resource\Model\TimestampableTrait; -/** - * @mixin ChannelPricing - */ trait ChannelPricingTrait { use TimestampableTrait; /** @ORM\Column(type="boolean", options={"default": 0}) */ + #[ORM\Column(type: 'boolean', options: ['default' => 0])] protected bool $manuallyDiscounted = false; - /** - * @ORM\Column(type="decimal", precision=8, scale=4, options={"default": 1}) - * - * @var float - */ - protected $multiplier = 1; - - /** @ORM\Column(type="string", nullable=true) */ - protected ?string $bulkIdentifier = null; - - /** - * @ORM\Column(type="datetime", nullable=true) - * - * @Gedmo\Timestampable(on="create") - * - * @var DateTimeInterface|null - */ - protected $createdAt; - - /** - * @ORM\Column(type="datetime", nullable=true) - * - * @Gedmo\Timestampable(on="update") - * - * @var DateTimeInterface|null - */ - protected $updatedAt; - - public function hasDiscount(): bool - { - return null !== $this->getOriginalPrice() && - null !== $this->getPrice() && - $this->getOriginalPrice() > $this->getPrice() - ; - } - - public function getDiscountAmount(): ?int - { - if (!$this->hasDiscount()) { - return null; - } - - return (int) $this->getOriginalPrice() - (int) $this->getPrice(); - } - - public function getDisplayableDiscount(bool $asInteger = false): ?float - { - if (!$this->hasDiscount()) { - return null; - } - - $precision = $asInteger ? 0 : 2; - - return round(100 - ($this->getPrice() / $this->getOriginalPrice() * 100), $precision); - } - public function isManuallyDiscounted(): bool { return $this->manuallyDiscounted; @@ -84,24 +24,5 @@ public function isManuallyDiscounted(): bool public function setManuallyDiscounted(bool $manuallyDiscounted): void { $this->manuallyDiscounted = $manuallyDiscounted; - - // when a user is manually changing the prices, we don't want this channel pricing to be part of any bulk updates - $this->bulkIdentifier = null; - $this->multiplier = 1; - } - - public function getMultiplier(): float - { - return $this->multiplier; - } - - public function getBulkIdentifier(): ?string - { - return $this->bulkIdentifier; - } - - public function resetBulkIdentifier(): void - { - $this->bulkIdentifier = null; } } diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 891c449..dcf8d73 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -3,7 +3,6 @@ - diff --git a/src/Resources/config/services/event_listener.xml b/src/Resources/config/services/event_listener.xml deleted file mode 100644 index 563c4da..0000000 --- a/src/Resources/config/services/event_listener.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - %sylius.model.channel_pricing.class% - %sylius.model.product.class% - %sylius.model.product_variant.class% - - - - - diff --git a/tests/Application/.env b/tests/Application/.env index d251157..d2cd161 100644 --- a/tests/Application/.env +++ b/tests/Application/.env @@ -23,13 +23,13 @@ APP_SECRET=EDITME # Format described at https://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url # For a sqlite database, use: "sqlite:///%kernel.project_dir%/var/data.db" # Set "serverVersion" to your server version to avoid edge-case exceptions and extra database calls -DATABASE_URL=mysql://root@127.0.0.1/acme_sylius_example_%kernel.environment%?serverVersion=5.7 +DATABASE_URL=mysql://root@127.0.0.1/setono_sylius_catalog_promotion_%kernel.environment%?serverVersion=5.7 ###< doctrine/doctrine-bundle ### ###> lexik/jwt-authentication-bundle ### JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem -JWT_PASSPHRASE=acme_plugin_development +JWT_PASSPHRASE=setono_plugin_development ###< lexik/jwt-authentication-bundle ### ###> symfony/messenger ### diff --git a/tests/Application/Entity/ChannelPricing.php b/tests/Application/Model/ChannelPricing.php similarity index 98% rename from tests/Application/Entity/ChannelPricing.php rename to tests/Application/Model/ChannelPricing.php index b87db7b..84f34f3 100644 --- a/tests/Application/Entity/ChannelPricing.php +++ b/tests/Application/Model/ChannelPricing.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Setono\SyliusCatalogPromotionPlugin\Tests\Application\Entity; +namespace Setono\SyliusCatalogPromotionPlugin\Tests\Application\Model; use Doctrine\ORM\Mapping as ORM; use Setono\SyliusCatalogPromotionPlugin\Model\ChannelPricingInterface as CatalogPromotionChannelPricingInterface; diff --git a/tests/Application/config/packages/_sylius.yaml b/tests/Application/config/packages/_sylius.yaml index a78e725..7927f0b 100644 --- a/tests/Application/config/packages/_sylius.yaml +++ b/tests/Application/config/packages/_sylius.yaml @@ -13,3 +13,10 @@ sylius_shop: sylius_api: enabled: true + + +sylius_core: + resources: + channel_pricing: + classes: + model: Setono\SyliusCatalogPromotionPlugin\Tests\Application\Model\ChannelPricing diff --git a/tests/Application/config/packages/doctrine.yaml b/tests/Application/config/packages/doctrine.yaml index 117b111..3e40ed5 100644 --- a/tests/Application/config/packages/doctrine.yaml +++ b/tests/Application/config/packages/doctrine.yaml @@ -13,12 +13,12 @@ doctrine: url: '%env(resolve:DATABASE_URL)%' orm: - auto_generate_proxy_classes: true - naming_strategy: doctrine.orm.naming_strategy.underscore + auto_generate_proxy_classes: '%kernel.debug%' auto_mapping: true mappings: App: is_bundle: false type: annotation - dir: '%kernel.project_dir%/Entity' - prefix: 'Setono\SyliusCatalogPromotionPlugin\Tests\Application\Entity' + dir: '%kernel.project_dir%/Model' + prefix: 'Setono\SyliusCatalogPromotionPlugin\Tests\Application\Model' + alias: App