From edf9ce83b7cd32f975223a7da8f26cb87a7b2c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Thu, 9 Jan 2025 09:18:28 +0100 Subject: [PATCH] Add triggeredBy property to CatalogPromotionUpdate entity and fix a bug in dispatching of the StartCatalogPromotionUpdate message --- src/Command/UpdateCommand.php | 2 +- .../SetonoSyliusCatalogPromotionExtension.php | 5 +++++ .../UpdateCatalogPromotionSubscriber.php | 14 ++++++++------ src/EventSubscriber/UpdateProductSubscriber.php | 14 ++++++++------ src/Factory/CatalogPromotionUpdateFactory.php | 8 +++++--- .../CatalogPromotionUpdateFactoryInterface.php | 7 ++----- .../Command/StartCatalogPromotionUpdate.php | 4 ++++ .../StartCatalogPromotionUpdateHandler.php | 2 +- src/Model/CatalogPromotionUpdate.php | 12 ++++++++++++ src/Model/CatalogPromotionUpdateInterface.php | 7 +++++++ .../doctrine/model/CatalogPromotionUpdate.orm.xml | 3 ++- src/Resources/translations/messages.en.yaml | 3 ++- 12 files changed, 57 insertions(+), 24 deletions(-) diff --git a/src/Command/UpdateCommand.php b/src/Command/UpdateCommand.php index ad5f476..7a6690f 100644 --- a/src/Command/UpdateCommand.php +++ b/src/Command/UpdateCommand.php @@ -23,7 +23,7 @@ public function __construct(private readonly MessageBusInterface $commandBus) protected function execute(InputInterface $input, OutputInterface $output): int { - $this->commandBus->dispatch(new StartCatalogPromotionUpdate()); + $this->commandBus->dispatch(new StartCatalogPromotionUpdate(triggeredBy: sprintf('Symfony command "%s"', (string) self::$defaultName))); return 0; } diff --git a/src/DependencyInjection/SetonoSyliusCatalogPromotionExtension.php b/src/DependencyInjection/SetonoSyliusCatalogPromotionExtension.php index 02d533a..0f636b1 100644 --- a/src/DependencyInjection/SetonoSyliusCatalogPromotionExtension.php +++ b/src/DependencyInjection/SetonoSyliusCatalogPromotionExtension.php @@ -66,6 +66,11 @@ public function prepend(ContainerBuilder $container): void 'label' => 'sylius.ui.state', 'sortable' => null, ], + 'triggeredBy' => [ + 'type' => 'string', + 'label' => 'setono_sylius_catalog_promotion.ui.triggered_by', + 'sortable' => null, + ], 'catalogPromotions' => [ 'type' => 'twig', 'label' => 'setono_sylius_catalog_promotion.ui.catalog_promotions', diff --git a/src/EventSubscriber/UpdateCatalogPromotionSubscriber.php b/src/EventSubscriber/UpdateCatalogPromotionSubscriber.php index 380b4c0..37eb3ea 100644 --- a/src/EventSubscriber/UpdateCatalogPromotionSubscriber.php +++ b/src/EventSubscriber/UpdateCatalogPromotionSubscriber.php @@ -12,14 +12,13 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\Messenger\MessageBusInterface; -use Symfony\Contracts\Service\ResetInterface; use Webmozart\Assert\Assert; /** * Notice that we don't need to handle the removal of catalog promotions because although the catalog promotion * might be pre-qualified, it will not be applied to any products (because it doesn't exist anymore) */ -final class UpdateCatalogPromotionSubscriber implements EventSubscriberInterface, ResetInterface +final class UpdateCatalogPromotionSubscriber implements EventSubscriberInterface { /** * A list of catalog promotions to update @@ -76,11 +75,14 @@ public function dispatch(): void return; } - $this->commandBus->dispatch(new StartCatalogPromotionUpdate(catalogPromotions: $this->catalogPromotions)); - } + $this->commandBus->dispatch(new StartCatalogPromotionUpdate( + catalogPromotions: $this->catalogPromotions, + triggeredBy: sprintf( + 'The update/creation of the following catalog promotions: "%s"', + implode('", "', array_map(static fn (PromotionInterface $promotion): string => (string) ($promotion->getName() ?? $promotion->getCode()), $this->catalogPromotions)), + ), + )); - public function reset(): void - { $this->catalogPromotions = []; } } diff --git a/src/EventSubscriber/UpdateProductSubscriber.php b/src/EventSubscriber/UpdateProductSubscriber.php index 0111572..ddb7122 100644 --- a/src/EventSubscriber/UpdateProductSubscriber.php +++ b/src/EventSubscriber/UpdateProductSubscriber.php @@ -12,10 +12,9 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\Messenger\MessageBusInterface; -use Symfony\Contracts\Service\ResetInterface; use Webmozart\Assert\Assert; -final class UpdateProductSubscriber implements EventSubscriberInterface, ResetInterface +final class UpdateProductSubscriber implements EventSubscriberInterface { /** * A list of products to update @@ -72,11 +71,14 @@ public function dispatch(): void return; } - $this->commandBus->dispatch(new StartCatalogPromotionUpdate(products: $this->products)); - } + $this->commandBus->dispatch(new StartCatalogPromotionUpdate( + products: $this->products, + triggeredBy: sprintf( + 'The update/creation of the following products: "%s"', + implode('", "', array_map(static fn (ProductInterface $product): string => (string) ($product->getName() ?? $product->getCode()), $this->products)), + ), + )); - public function reset(): void - { $this->products = []; } } diff --git a/src/Factory/CatalogPromotionUpdateFactory.php b/src/Factory/CatalogPromotionUpdateFactory.php index 71b41c1..f82d156 100644 --- a/src/Factory/CatalogPromotionUpdateFactory.php +++ b/src/Factory/CatalogPromotionUpdateFactory.php @@ -4,6 +4,7 @@ namespace Setono\SyliusCatalogPromotionPlugin\Factory; +use Setono\SyliusCatalogPromotionPlugin\Message\Command\StartCatalogPromotionUpdate; use Setono\SyliusCatalogPromotionPlugin\Model\CatalogPromotionUpdateInterface; use Sylius\Component\Resource\Factory\FactoryInterface; use Webmozart\Assert\Assert; @@ -22,11 +23,12 @@ public function createNew(): CatalogPromotionUpdateInterface return $obj; } - public function createWithCatalogPromotionsAndProducts(array $catalogPromotions, array $products): CatalogPromotionUpdateInterface + public function createFromMessage(StartCatalogPromotionUpdate $message): CatalogPromotionUpdateInterface { $obj = $this->createNew(); - $obj->setCatalogPromotions($catalogPromotions); - $obj->setProducts($products); + $obj->setCatalogPromotions($message->catalogPromotions); + $obj->setProducts($message->products); + $obj->setTriggeredBy($message->triggeredBy); return $obj; } diff --git a/src/Factory/CatalogPromotionUpdateFactoryInterface.php b/src/Factory/CatalogPromotionUpdateFactoryInterface.php index 3934221..98600cf 100644 --- a/src/Factory/CatalogPromotionUpdateFactoryInterface.php +++ b/src/Factory/CatalogPromotionUpdateFactoryInterface.php @@ -4,6 +4,7 @@ namespace Setono\SyliusCatalogPromotionPlugin\Factory; +use Setono\SyliusCatalogPromotionPlugin\Message\Command\StartCatalogPromotionUpdate; use Setono\SyliusCatalogPromotionPlugin\Model\CatalogPromotionUpdateInterface; use Sylius\Component\Resource\Factory\FactoryInterface; @@ -11,9 +12,5 @@ interface CatalogPromotionUpdateFactoryInterface extends FactoryInterface { public function createNew(): CatalogPromotionUpdateInterface; - /** - * @param list $catalogPromotions - * @param list $products - */ - public function createWithCatalogPromotionsAndProducts(array $catalogPromotions, array $products): CatalogPromotionUpdateInterface; + public function createFromMessage(StartCatalogPromotionUpdate $message): CatalogPromotionUpdateInterface; } diff --git a/src/Message/Command/StartCatalogPromotionUpdate.php b/src/Message/Command/StartCatalogPromotionUpdate.php index 5631387..681ecd1 100644 --- a/src/Message/Command/StartCatalogPromotionUpdate.php +++ b/src/Message/Command/StartCatalogPromotionUpdate.php @@ -33,6 +33,10 @@ final class StartCatalogPromotionUpdate public function __construct( array $catalogPromotions = [], array $products = [], + /** + * If you want to give information about what started the update, you can provide a string here + */ + public readonly ?string $triggeredBy = null, ) { $this->catalogPromotions = array_values(array_unique(array_map( static fn (string|PromotionInterface $catalogPromotion) => $catalogPromotion instanceof PromotionInterface ? (string) $catalogPromotion->getCode() : $catalogPromotion, diff --git a/src/Message/CommandHandler/StartCatalogPromotionUpdateHandler.php b/src/Message/CommandHandler/StartCatalogPromotionUpdateHandler.php index a0a41b3..22bd11e 100644 --- a/src/Message/CommandHandler/StartCatalogPromotionUpdateHandler.php +++ b/src/Message/CommandHandler/StartCatalogPromotionUpdateHandler.php @@ -28,7 +28,7 @@ public function __construct( public function __invoke(StartCatalogPromotionUpdate $message): CatalogPromotionUpdateInterface { - $catalogPromotionUpdate = $this->catalogPromotionUpdateFactory->createWithCatalogPromotionsAndProducts($message->catalogPromotions, $message->products); + $catalogPromotionUpdate = $this->catalogPromotionUpdateFactory->createFromMessage($message); $manager = $this->getManager($catalogPromotionUpdate); $manager->persist($catalogPromotionUpdate); diff --git a/src/Model/CatalogPromotionUpdate.php b/src/Model/CatalogPromotionUpdate.php index eaec557..ab8be90 100644 --- a/src/Model/CatalogPromotionUpdate.php +++ b/src/Model/CatalogPromotionUpdate.php @@ -24,6 +24,8 @@ class CatalogPromotionUpdate implements CatalogPromotionUpdateInterface /** @var list|null */ protected ?array $products = null; + protected ?string $triggeredBy = null; + protected ?int $productsEligibleForUpdate = null; protected int $productsUpdated = 0; @@ -97,6 +99,16 @@ public function setProducts(?array $products): void $this->products = $products; } + public function getTriggeredBy(): ?string + { + return $this->triggeredBy; + } + + public function setTriggeredBy(?string $triggeredBy): void + { + $this->triggeredBy = $triggeredBy; + } + public function getProductsEligibleForUpdate(): ?int { return $this->productsEligibleForUpdate; diff --git a/src/Model/CatalogPromotionUpdateInterface.php b/src/Model/CatalogPromotionUpdateInterface.php index 08fc940..7cb9021 100644 --- a/src/Model/CatalogPromotionUpdateInterface.php +++ b/src/Model/CatalogPromotionUpdateInterface.php @@ -52,6 +52,13 @@ public function getProducts(): array; */ public function setProducts(?array $products): void; + /** + * Information about what started the update + */ + public function getTriggeredBy(): ?string; + + public function setTriggeredBy(?string $triggeredBy): void; + /** * todo do we need this? * The number of products that are eligible for update. This should be set when the update is created diff --git a/src/Resources/config/doctrine/model/CatalogPromotionUpdate.orm.xml b/src/Resources/config/doctrine/model/CatalogPromotionUpdate.orm.xml index b341d7b..6e43545 100644 --- a/src/Resources/config/doctrine/model/CatalogPromotionUpdate.orm.xml +++ b/src/Resources/config/doctrine/model/CatalogPromotionUpdate.orm.xml @@ -14,8 +14,9 @@ - + + diff --git a/src/Resources/translations/messages.en.yaml b/src/Resources/translations/messages.en.yaml index 1b15302..fc1fa67 100644 --- a/src/Resources/translations/messages.en.yaml +++ b/src/Resources/translations/messages.en.yaml @@ -34,6 +34,7 @@ setono_sylius_catalog_promotion: marketing: promotions: Catalog promotions ui: + catalog_promotion_updates: Catalog promotion updates catalog_promotions: Catalog promotions edit_promotion: Edit catalog promotion manage_promotions: Manage catalog promotions @@ -41,5 +42,5 @@ setono_sylius_catalog_promotion: promotion: Catalog promotion promotions: Catalog promotions rules_explanation: If you don't add any rules, the promotion will be applied to all products + triggered_by: Triggered by updated_at: Updated at - catalog_promotion_updates: Catalog promotion updates