Skip to content

Commit

Permalink
Add triggeredBy property to CatalogPromotionUpdate entity and fix a b…
Browse files Browse the repository at this point in the history
…ug in dispatching of the StartCatalogPromotionUpdate message
  • Loading branch information
loevgaard committed Jan 9, 2025
1 parent 45e2980 commit edf9ce8
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Command/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
14 changes: 8 additions & 6 deletions src/EventSubscriber/UpdateCatalogPromotionSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [];
}
}
14 changes: 8 additions & 6 deletions src/EventSubscriber/UpdateProductSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [];
}
}
8 changes: 5 additions & 3 deletions src/Factory/CatalogPromotionUpdateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
7 changes: 2 additions & 5 deletions src/Factory/CatalogPromotionUpdateFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

namespace Setono\SyliusCatalogPromotionPlugin\Factory;

use Setono\SyliusCatalogPromotionPlugin\Message\Command\StartCatalogPromotionUpdate;
use Setono\SyliusCatalogPromotionPlugin\Model\CatalogPromotionUpdateInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

interface CatalogPromotionUpdateFactoryInterface extends FactoryInterface
{
public function createNew(): CatalogPromotionUpdateInterface;

/**
* @param list<string> $catalogPromotions
* @param list<int> $products
*/
public function createWithCatalogPromotionsAndProducts(array $catalogPromotions, array $products): CatalogPromotionUpdateInterface;
public function createFromMessage(StartCatalogPromotionUpdate $message): CatalogPromotionUpdateInterface;
}
4 changes: 4 additions & 0 deletions src/Message/Command/StartCatalogPromotionUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/Model/CatalogPromotionUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class CatalogPromotionUpdate implements CatalogPromotionUpdateInterface
/** @var list<int>|null */
protected ?array $products = null;

protected ?string $triggeredBy = null;

protected ?int $productsEligibleForUpdate = null;

protected int $productsUpdated = 0;
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/Model/CatalogPromotionUpdateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
<field name="version" type="integer" version="true"/>
<field name="state"/>
<field name="error" type="text" nullable="true"/>
<field name="catalogPromotions" type="json"/>
<field name="catalogPromotions" type="json" nullable="true"/>
<field name="products" type="json" nullable="true"/>
<field name="triggeredBy" type="text" nullable="true"/>
<field name="productsEligibleForUpdate" type="integer" nullable="true"/>
<field name="productsUpdated" type="integer"/>
<field name="messageIds" type="json" nullable="true"/>
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ 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
new_promotion: Create new 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

0 comments on commit edf9ce8

Please sign in to comment.