vendor/shopware/core/Content/Flow/Dispatching/FlowDispatcher.php line 54

Open in your IDE?
  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\BusinessEvent;
  9. use Shopware\Core\Framework\Event\FlowEvent;
  10. use Shopware\Core\Framework\Event\FlowEventAware;
  11. use Shopware\Core\Framework\Event\FlowLogEvent;
  12. use Shopware\Core\Framework\Feature;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. /**
  18.  * @internal not intended for decoration or replacement
  19.  */
  20. class FlowDispatcher implements EventDispatcherInterface
  21. {
  22.     private EventDispatcherInterface $dispatcher;
  23.     private ContainerInterface $container;
  24.     private LoggerInterface $logger;
  25.     private FlowFactory $flowFactory;
  26.     public function __construct(EventDispatcherInterface $dispatcherLoggerInterface $loggerFlowFactory $flowFactory)
  27.     {
  28.         $this->dispatcher $dispatcher;
  29.         $this->logger $logger;
  30.         $this->flowFactory $flowFactory;
  31.     }
  32.     public function setContainer(ContainerInterface $container): void
  33.     {
  34.         $this->container $container;
  35.     }
  36.     /**
  37.      * @template TEvent of object
  38.      *
  39.      * @param TEvent $event
  40.      *
  41.      * @return TEvent
  42.      */
  43.     public function dispatch($event, ?string $eventName null): object
  44.     {
  45.         $event $this->dispatcher->dispatch($event$eventName);
  46.         if (!$event instanceof FlowEventAware) {
  47.             return $event;
  48.         }
  49.         if (Feature::isActive('v6.5.0.0')) {
  50.             $flowLogEvent = new FlowLogEvent(FlowLogEvent::NAME$event);
  51.             $this->dispatcher->dispatch($flowLogEvent$flowLogEvent->getName());
  52.         }
  53.         if (Feature::isActive('FEATURE_NEXT_17858')) {
  54.             if ($event instanceof FlowEvent) {
  55.                 return $event;
  56.             }
  57.         } else {
  58.             if ($event instanceof BusinessEvent || $event instanceof FlowEvent) {
  59.                 return $event;
  60.             }
  61.         }
  62.         if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
  63.             return $event;
  64.         }
  65.         if ($event->getContext()->hasState(Context::SKIP_TRIGGER_FLOW)) {
  66.             return $event;
  67.         }
  68.         $storableFlow $this->flowFactory->create($event);
  69.         /** @deprecated tag:v6.5.0 Will be removed */
  70.         if (!Feature::isActive('v6.5.0.0')) {
  71.             $storableFlow->setOriginalEvent($event);
  72.         }
  73.         $this->callFlowExecutor($storableFlow);
  74.         return $event;
  75.     }
  76.     /**
  77.      * @param string   $eventName
  78.      * @param callable $listener
  79.      * @param int      $priority
  80.      */
  81.     public function addListener($eventName$listener$priority 0): void
  82.     {
  83.         $this->dispatcher->addListener($eventName$listener$priority);
  84.     }
  85.     public function addSubscriber(EventSubscriberInterface $subscriber): void
  86.     {
  87.         $this->dispatcher->addSubscriber($subscriber);
  88.     }
  89.     /**
  90.      * @param string   $eventName
  91.      * @param callable $listener
  92.      */
  93.     public function removeListener($eventName$listener): void
  94.     {
  95.         $this->dispatcher->removeListener($eventName$listener);
  96.     }
  97.     public function removeSubscriber(EventSubscriberInterface $subscriber): void
  98.     {
  99.         $this->dispatcher->removeSubscriber($subscriber);
  100.     }
  101.     public function getListeners(?string $eventName null): array
  102.     {
  103.         return $this->dispatcher->getListeners($eventName);
  104.     }
  105.     /**
  106.      * @param string $eventName
  107.      * @param callable $listener
  108.      */
  109.     public function getListenerPriority($eventName$listener): ?int
  110.     {
  111.         return $this->dispatcher->getListenerPriority($eventName$listener);
  112.     }
  113.     public function hasListeners(?string $eventName null): bool
  114.     {
  115.         return $this->dispatcher->hasListeners($eventName);
  116.     }
  117.     private function callFlowExecutor(StorableFlow $event): void
  118.     {
  119.         $flows $this->getFlows($event->getName());
  120.         if (empty($flows)) {
  121.             return;
  122.         }
  123.         /** @var FlowExecutor|null $flowExecutor */
  124.         $flowExecutor $this->container->get(FlowExecutor::class);
  125.         if ($flowExecutor === null) {
  126.             throw new ServiceNotFoundException(FlowExecutor::class);
  127.         }
  128.         foreach ($flows as $flow) {
  129.             try {
  130.                 /** @var Flow $payload */
  131.                 $payload $flow['payload'];
  132.                 $flowExecutor->execute($payload$event);
  133.             } catch (ExecuteSequenceException $e) {
  134.                 $this->logger->error(
  135.                     "Could not execute flow with error message:\n"
  136.                     'Flow name: ' $flow['name'] . "\n"
  137.                     'Flow id: ' $flow['id'] . "\n"
  138.                     'Sequence id: ' $e->getSequenceId() . "\n"
  139.                     $e->getMessage() . "\n"
  140.                     'Error Code: ' $e->getCode() . "\n"
  141.                 );
  142.             } catch (\Throwable $e) {
  143.                 $this->logger->error(
  144.                     "Could not execute flow with error message:\n"
  145.                     'Flow name: ' $flow['name'] . "\n"
  146.                     'Flow id: ' $flow['id'] . "\n"
  147.                     $e->getMessage() . "\n"
  148.                     'Error Code: ' $e->getCode() . "\n"
  149.                 );
  150.             }
  151.         }
  152.     }
  153.     /**
  154.      * @return array<string, mixed>
  155.      */
  156.     private function getFlows(string $eventName): array
  157.     {
  158.         /** @var AbstractFlowLoader|null $flowLoader */
  159.         $flowLoader $this->container->get(FlowLoader::class);
  160.         if ($flowLoader === null) {
  161.             throw new ServiceNotFoundException(FlowExecutor::class);
  162.         }
  163.         $flows $flowLoader->load();
  164.         $result = [];
  165.         if (\array_key_exists($eventName$flows)) {
  166.             $result $flows[$eventName];
  167.         }
  168.         return $result;
  169.     }
  170. }