Skip to content

Commit ac3e4fa

Browse files
committed
Handle catalog promotion updates
1 parent e2f94b3 commit ac3e4fa

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusCatalogPromotionPlugin\EventSubscriber;
6+
7+
use Doctrine\Persistence\Event\LifecycleEventArgs;
8+
use Setono\SyliusCatalogPromotionPlugin\Message\Command\StartCatalogPromotionUpdate;
9+
use Setono\SyliusCatalogPromotionPlugin\Model\PromotionInterface;
10+
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
11+
use Symfony\Component\Console\ConsoleEvents;
12+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13+
use Symfony\Component\HttpKernel\KernelEvents;
14+
use Symfony\Component\Messenger\MessageBusInterface;
15+
use Symfony\Contracts\Service\ResetInterface;
16+
use Webmozart\Assert\Assert;
17+
18+
/**
19+
* Notice that we don't need to handle the removal of catalog promotions because although the catalog promotion
20+
* might be pre-qualified, it will not be applied to any products (because it doesn't exist anymore)
21+
*/
22+
final class UpdateCatalogPromotionSubscriber implements EventSubscriberInterface, ResetInterface
23+
{
24+
/**
25+
* A list of catalog promotions to update
26+
*
27+
* @var list<PromotionInterface>
28+
*/
29+
private array $catalogPromotions = [];
30+
31+
public function __construct(private readonly MessageBusInterface $commandBus)
32+
{
33+
}
34+
35+
public static function getSubscribedEvents(): array
36+
{
37+
return [
38+
'setono_sylius_catalog_promotion.promotion.post_create' => 'update',
39+
'setono_sylius_catalog_promotion.promotion.post_update' => 'update',
40+
KernelEvents::TERMINATE => 'dispatch',
41+
ConsoleEvents::TERMINATE => 'dispatch',
42+
];
43+
}
44+
45+
public function update(ResourceControllerEvent $event): void
46+
{
47+
$obj = $event->getSubject();
48+
Assert::isInstanceOf($obj, PromotionInterface::class);
49+
50+
$this->catalogPromotions[] = $obj;
51+
}
52+
53+
public function postPersist(LifecycleEventArgs $eventArgs): void
54+
{
55+
$obj = $eventArgs->getObject();
56+
if (!$obj instanceof PromotionInterface) {
57+
return;
58+
}
59+
60+
$this->catalogPromotions[] = $obj;
61+
}
62+
63+
public function postUpdate(LifecycleEventArgs $eventArgs): void
64+
{
65+
$obj = $eventArgs->getObject();
66+
if (!$obj instanceof PromotionInterface) {
67+
return;
68+
}
69+
70+
$this->catalogPromotions[] = $obj;
71+
}
72+
73+
public function dispatch(): void
74+
{
75+
if ([] === $this->catalogPromotions) {
76+
return;
77+
}
78+
79+
$this->commandBus->dispatch(new StartCatalogPromotionUpdate(catalogPromotions: $this->catalogPromotions));
80+
}
81+
82+
public function reset(): void
83+
{
84+
$this->catalogPromotions = [];
85+
}
86+
}

src/Resources/config/services/event_subscriber.xml

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
<tag name="kernel.event_subscriber"/>
99
</service>
1010

11+
<service id="Setono\SyliusCatalogPromotionPlugin\EventSubscriber\UpdateCatalogPromotionSubscriber">
12+
<argument type="service" id="setono_sylius_catalog_promotion.command_bus"/>
13+
14+
<tag name="kernel.event_subscriber"/>
15+
<tag name="doctrine.event_listener" event="postPersist"/>
16+
<tag name="doctrine.event_listener" event="postUpdate"/>
17+
</service>
18+
1119
<service id="Setono\SyliusCatalogPromotionPlugin\EventSubscriber\UpdateProductSubscriber">
1220
<argument type="service" id="setono_sylius_catalog_promotion.command_bus"/>
1321

0 commit comments

Comments
 (0)