src/Core/Content/Flow/Dispatching/FlowDispatcher.php line 44

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Flow\Dispatching;
  3. use Psr\EventDispatcher\StoppableEventInterface;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Content\Flow\Dispatching\Struct\Flow;
  6. use Shopware\Core\Content\Flow\Exception\ExecuteSequenceException;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\Event\FlowEventAware;
  9. use Shopware\Core\Framework\Event\FlowLogEvent;
  10. use Shopware\Core\Framework\Log\Package;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. /**
  16.  * @internal not intended for decoration or replacement
  17.  */
  18. #[Package('business-ops')]
  19. class FlowDispatcher implements EventDispatcherInterface
  20. {
  21.     private ContainerInterface $container;
  22.     public function __construct(private readonly EventDispatcherInterface $dispatcher, private readonly LoggerInterface $logger, private readonly FlowFactory $flowFactory)
  23.     {
  24.     }
  25.     public function setContainer(ContainerInterface $container): void
  26.     {
  27.         $this->container $container;
  28.     }
  29.     /**
  30.      * @template TEvent of object
  31.      *
  32.      * @param TEvent $event
  33.      *
  34.      * @return TEvent
  35.      */
  36.     public function dispatch($event, ?string $eventName null): object
  37.     {
  38.         $event $this->dispatcher->dispatch($event$eventName);
  39.         if (!$event instanceof FlowEventAware) {
  40.             return $event;
  41.         }
  42.         $flowLogEvent = new FlowLogEvent(FlowLogEvent::NAME$event);
  43.         $this->dispatcher->dispatch($flowLogEvent$flowLogEvent->getName());
  44.         if (($event instanceof StoppableEventInterface && $event->isPropagationStopped())
  45.             || $event->getContext()->hasState(Context::SKIP_TRIGGER_FLOW)
  46.         ) {
  47.             return $event;
  48.         }
  49.         $storableFlow $this->flowFactory->create($event);
  50.         $this->callFlowExecutor($storableFlow);
  51.         return $event;
  52.     }
  53.     /**
  54.      * @param callable $listener
  55.      */
  56.     public function addListener(string $eventName$listenerint $priority 0): void
  57.     {
  58.         $this->dispatcher->addListener($eventName$listener$priority);
  59.     }
  60.     public function addSubscriber(EventSubscriberInterface $subscriber): void
  61.     {
  62.         $this->dispatcher->addSubscriber($subscriber);
  63.     }
  64.     /**
  65.      * @param callable $listener
  66.      */
  67.     public function removeListener(string $eventName$listener): void
  68.     {
  69.         $this->dispatcher->removeListener($eventName$listener);
  70.     }
  71.     public function removeSubscriber(EventSubscriberInterface $subscriber): void
  72.     {
  73.         $this->dispatcher->removeSubscriber($subscriber);
  74.     }
  75.     public function getListeners(?string $eventName null): array
  76.     {
  77.         return $this->dispatcher->getListeners($eventName);
  78.     }
  79.     /**
  80.      * @param callable $listener
  81.      */
  82.     public function getListenerPriority(string $eventName$listener): ?int
  83.     {
  84.         return $this->dispatcher->getListenerPriority($eventName$listener);
  85.     }
  86.     public function hasListeners(?string $eventName null): bool
  87.     {
  88.         return $this->dispatcher->hasListeners($eventName);
  89.     }
  90.     private function callFlowExecutor(StorableFlow $event): void
  91.     {
  92.         $flows $this->getFlows($event->getName());
  93.         if (empty($flows)) {
  94.             return;
  95.         }
  96.         /** @var FlowExecutor|null $flowExecutor */
  97.         $flowExecutor $this->container->get(FlowExecutor::class);
  98.         if ($flowExecutor === null) {
  99.             throw new ServiceNotFoundException(FlowExecutor::class);
  100.         }
  101.         foreach ($flows as $flow) {
  102.             try {
  103.                 /** @var Flow $payload */
  104.                 $payload $flow['payload'];
  105.                 $flowExecutor->execute($payload$event);
  106.             } catch (ExecuteSequenceException $e) {
  107.                 $this->logger->error(
  108.                     "Could not execute flow with error message:\n"
  109.                     'Flow name: ' $flow['name'] . "\n"
  110.                     'Flow id: ' $flow['id'] . "\n"
  111.                     'Sequence id: ' $e->getSequenceId() . "\n"
  112.                     $e->getMessage() . "\n"
  113.                     'Error Code: ' $e->getCode() . "\n"
  114.                 );
  115.             } catch (\Throwable $e) {
  116.                 $this->logger->error(
  117.                     "Could not execute flow with error message:\n"
  118.                     'Flow name: ' $flow['name'] . "\n"
  119.                     'Flow id: ' $flow['id'] . "\n"
  120.                     $e->getMessage() . "\n"
  121.                     'Error Code: ' $e->getCode() . "\n"
  122.                 );
  123.             }
  124.         }
  125.     }
  126.     /**
  127.      * @return array<string, mixed>
  128.      */
  129.     private function getFlows(string $eventName): array
  130.     {
  131.         /** @var AbstractFlowLoader|null $flowLoader */
  132.         $flowLoader $this->container->get(FlowLoader::class);
  133.         if ($flowLoader === null) {
  134.             throw new ServiceNotFoundException(FlowExecutor::class);
  135.         }
  136.         $flows $flowLoader->load();
  137.         $result = [];
  138.         if (\array_key_exists($eventName$flows)) {
  139.             $result $flows[$eventName];
  140.         }
  141.         return $result;
  142.     }
  143. }