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

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