diff --git a/src/Factory/CatalogPromotionUpdateFactory.php b/src/Factory/CatalogPromotionUpdateFactory.php index b0f9df0..71b41c1 100644 --- a/src/Factory/CatalogPromotionUpdateFactory.php +++ b/src/Factory/CatalogPromotionUpdateFactory.php @@ -22,10 +22,11 @@ public function createNew(): CatalogPromotionUpdateInterface return $obj; } - public function createWithCatalogPromotions(array $catalogPromotions): CatalogPromotionUpdateInterface + public function createWithCatalogPromotionsAndProducts(array $catalogPromotions, array $products): CatalogPromotionUpdateInterface { $obj = $this->createNew(); $obj->setCatalogPromotions($catalogPromotions); + $obj->setProducts($products); return $obj; } diff --git a/src/Factory/CatalogPromotionUpdateFactoryInterface.php b/src/Factory/CatalogPromotionUpdateFactoryInterface.php index 92f638e..3934221 100644 --- a/src/Factory/CatalogPromotionUpdateFactoryInterface.php +++ b/src/Factory/CatalogPromotionUpdateFactoryInterface.php @@ -12,7 +12,8 @@ interface CatalogPromotionUpdateFactoryInterface extends FactoryInterface public function createNew(): CatalogPromotionUpdateInterface; /** - * @param non-empty-list $catalogPromotions + * @param list $catalogPromotions + * @param list $products */ - public function createWithCatalogPromotions(array $catalogPromotions): CatalogPromotionUpdateInterface; + public function createWithCatalogPromotionsAndProducts(array $catalogPromotions, array $products): CatalogPromotionUpdateInterface; } diff --git a/src/Message/Command/UpdateProducts.php b/src/Message/Command/UpdateProducts.php index 40119d3..846460f 100644 --- a/src/Message/Command/UpdateProducts.php +++ b/src/Message/Command/UpdateProducts.php @@ -16,26 +16,15 @@ final class UpdateProducts implements AsyncCommandInterface public readonly string $messageId; - /** @var non-empty-list */ - public readonly array $catalogPromotions; - - /** - * @param list $catalogPromotions - */ public function __construct( CatalogPromotionUpdateInterface $catalogPromotionUpdate, - /** @var list $productIds */ + /** @var non-empty-list $productIds */ public readonly array $productIds, - array $catalogPromotions, + /** @var list $catalogPromotions */ + public readonly array $catalogPromotions, ) { $this->catalogPromotionUpdate = (int) $catalogPromotionUpdate->getId(); $this->messageId = (string) Uuid::v7(); $catalogPromotionUpdate->addMessageId($this->messageId); - - if ([] === $catalogPromotions) { - throw new \InvalidArgumentException('The catalog promotions array must not be empty'); - } - - $this->catalogPromotions = $catalogPromotions; } } diff --git a/src/Message/CommandHandler/StartCatalogPromotionUpdateHandler.php b/src/Message/CommandHandler/StartCatalogPromotionUpdateHandler.php index 6b5bb53..a0a41b3 100644 --- a/src/Message/CommandHandler/StartCatalogPromotionUpdateHandler.php +++ b/src/Message/CommandHandler/StartCatalogPromotionUpdateHandler.php @@ -10,9 +10,7 @@ use Setono\SyliusCatalogPromotionPlugin\Message\Command\ProcessCatalogPromotionUpdate; use Setono\SyliusCatalogPromotionPlugin\Message\Command\StartCatalogPromotionUpdate; use Setono\SyliusCatalogPromotionPlugin\Model\CatalogPromotionUpdateInterface; -use Setono\SyliusCatalogPromotionPlugin\Model\PromotionInterface; use Setono\SyliusCatalogPromotionPlugin\Repository\PromotionRepositoryInterface; -use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; use Symfony\Component\Messenger\MessageBusInterface; final class StartCatalogPromotionUpdateHandler @@ -30,22 +28,7 @@ public function __construct( public function __invoke(StartCatalogPromotionUpdate $message): CatalogPromotionUpdateInterface { - $catalogPromotions = $message->catalogPromotions; - if ([] === $message->catalogPromotions) { - $catalogPromotions = array_map( - static fn (PromotionInterface $promotion): string => (string) $promotion->getCode(), - $this->promotionRepository->findForProcessing($message->catalogPromotions), - ); - } - - if ([] === $catalogPromotions) { - throw new UnrecoverableMessageHandlingException('No catalog promotions found to process'); - } - - $catalogPromotionUpdate = $this->catalogPromotionUpdateFactory->createWithCatalogPromotions($catalogPromotions); - if ([] !== $message->products) { - $catalogPromotionUpdate->setProducts($message->products); - } + $catalogPromotionUpdate = $this->catalogPromotionUpdateFactory->createWithCatalogPromotionsAndProducts($message->catalogPromotions, $message->products); $manager = $this->getManager($catalogPromotionUpdate); $manager->persist($catalogPromotionUpdate); diff --git a/src/Model/CatalogPromotionUpdate.php b/src/Model/CatalogPromotionUpdate.php index df2455a..eaec557 100644 --- a/src/Model/CatalogPromotionUpdate.php +++ b/src/Model/CatalogPromotionUpdate.php @@ -74,8 +74,12 @@ public function getCatalogPromotions(): array return $this->catalogPromotions ?? []; } - public function setCatalogPromotions(array $catalogPromotions): void + public function setCatalogPromotions(?array $catalogPromotions): void { + if ([] === $catalogPromotions) { + $catalogPromotions = null; + } + $this->catalogPromotions = $catalogPromotions; } diff --git a/src/Model/CatalogPromotionUpdateInterface.php b/src/Model/CatalogPromotionUpdateInterface.php index 925e28b..08fc940 100644 --- a/src/Model/CatalogPromotionUpdateInterface.php +++ b/src/Model/CatalogPromotionUpdateInterface.php @@ -36,9 +36,9 @@ public function getError(): ?string; public function getCatalogPromotions(): array; /** - * @param non-empty-list $catalogPromotions + * @param list|null $catalogPromotions */ - public function setCatalogPromotions(array $catalogPromotions): void; + public function setCatalogPromotions(?array $catalogPromotions): void; /** * A list of product ids that the update is for. If empty, all products will be updated