<?php declare(strict_types=1);
namespace Zeobv\GetNotified\FlowBuilder\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\Event\BusinessEventCollector;
use Shopware\Core\Framework\Event\BusinessEventCollectorEvent;
use Shopware\Core\Framework\Event\BusinessEventCollectorResponse;
use Zeobv\GetNotified\FlowBuilder\Aware\ProductAware;
use Zeobv\GetNotified\FlowBuilder\Event\ProductBackInStockEvent;
class BusinessEventCollectorSubscriber implements EventSubscriberInterface
{
private BusinessEventCollector $businessEventCollector;
public function __construct(
BusinessEventCollector $businessEventCollector
) {
$this->businessEventCollector = $businessEventCollector;
}
public static function getSubscribedEvents(): array
{
return [
BusinessEventCollectorEvent::NAME => ['onAddEvent', 1000],
];
}
public function onAddEvent(BusinessEventCollectorEvent $event): void
{
$collection = $event->getCollection();
$this->setEventsInCollection($collection);
$this->setAwareDefinitions($collection);
}
protected function setEventsInCollection(BusinessEventCollectorResponse $collection): void
{
$definition = $this->businessEventCollector->define(ProductBackInStockEvent::class);
if (!$definition) {
return;
}
$collection->set($definition->getName(), $definition);
}
protected function setAwareDefinitions(BusinessEventCollectorResponse $collection): void
{
foreach ($collection->getElements() as $definition) {
if ($definition->getName() !== ProductBackInStockEvent::EVENT_NAME) {
continue;
}
$definition->addAware(ProductAware::class);
}
}
}