Skip to content

Commit

Permalink
Don't make it mandatory to have catalog promotions as input to the ca…
Browse files Browse the repository at this point in the history
…talog promotion update. It makes much more sense that if it's empty just process the catalog promotions that are active at that moment
  • Loading branch information
loevgaard committed Jan 8, 2025
1 parent 8634576 commit 45e2980
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 38 deletions.
3 changes: 2 additions & 1 deletion src/Factory/CatalogPromotionUpdateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Factory/CatalogPromotionUpdateFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ interface CatalogPromotionUpdateFactoryInterface extends FactoryInterface
public function createNew(): CatalogPromotionUpdateInterface;

/**
* @param non-empty-list<string> $catalogPromotions
* @param list<string> $catalogPromotions
* @param list<int> $products
*/
public function createWithCatalogPromotions(array $catalogPromotions): CatalogPromotionUpdateInterface;
public function createWithCatalogPromotionsAndProducts(array $catalogPromotions, array $products): CatalogPromotionUpdateInterface;
}
17 changes: 3 additions & 14 deletions src/Message/Command/UpdateProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,15 @@ final class UpdateProducts implements AsyncCommandInterface

public readonly string $messageId;

/** @var non-empty-list<string> */
public readonly array $catalogPromotions;

/**
* @param list<string> $catalogPromotions
*/
public function __construct(
CatalogPromotionUpdateInterface $catalogPromotionUpdate,
/** @var list<int> $productIds */
/** @var non-empty-list<int> $productIds */
public readonly array $productIds,
array $catalogPromotions,
/** @var list<string> $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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/Model/CatalogPromotionUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Model/CatalogPromotionUpdateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function getError(): ?string;
public function getCatalogPromotions(): array;

/**
* @param non-empty-list<string> $catalogPromotions
* @param list<string>|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
Expand Down

0 comments on commit 45e2980

Please sign in to comment.