vendor/shopware/core/Framework/Event/BusinessEventDispatcher.php line 156

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Event;
  3. use Psr\EventDispatcher\StoppableEventInterface;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
  10. use Shopware\Core\Framework\Event\EventAction\EventActionCollection;
  11. use Shopware\Core\Framework\Event\EventAction\EventActionDefinition;
  12. use Shopware\Core\Framework\Feature;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. /**
  16.  * @deprecated tag:v6.5.0 - reason:remove-decorator - Will be removed in v6.5.0, use FlowDispatcher instead.
  17.  */
  18. class BusinessEventDispatcher implements EventDispatcherInterface
  19. {
  20.     /**
  21.      * @var EventDispatcherInterface
  22.      */
  23.     private $dispatcher;
  24.     /**
  25.      * @var EventActionDefinition
  26.      */
  27.     private $eventActionDefinition;
  28.     /**
  29.      * @var DefinitionInstanceRegistry
  30.      */
  31.     private $definitionRegistry;
  32.     /**
  33.      * @internal
  34.      */
  35.     public function __construct(
  36.         EventDispatcherInterface $dispatcher,
  37.         DefinitionInstanceRegistry $definitionRegistry,
  38.         EventActionDefinition $eventActionDefinition
  39.     ) {
  40.         $this->dispatcher $dispatcher;
  41.         $this->eventActionDefinition $eventActionDefinition;
  42.         $this->definitionRegistry $definitionRegistry;
  43.     }
  44.     public function dispatch($event, ?string $eventName null): object
  45.     {
  46.         $event $this->dispatcher->dispatch($event$eventName);
  47.         if (Feature::isActive('FEATURE_NEXT_17858')) {
  48.             return $event;
  49.         }
  50.         if (!$event instanceof BusinessEventInterface || $event instanceof FlowEvent) {
  51.             return $event;
  52.         }
  53.         if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
  54.             return $event;
  55.         }
  56.         $this->callActions($event);
  57.         return $event;
  58.     }
  59.     /**
  60.      * @param callable $listener can not use native type declaration @see https://github.com/symfony/symfony/issues/42283
  61.      */
  62.     public function addListener(string $eventName$listenerint $priority 0): void
  63.     {
  64.         $this->dispatcher->addListener($eventName$listener$priority);
  65.     }
  66.     public function addSubscriber(EventSubscriberInterface $subscriber): void
  67.     {
  68.         $this->dispatcher->addSubscriber($subscriber);
  69.     }
  70.     /**
  71.      * @param callable $listener can not use native type hint as it is incompatible with symfony <5.3.4
  72.      */
  73.     public function removeListener(string $eventName$listener): void
  74.     {
  75.         $this->dispatcher->removeListener($eventName$listener);
  76.     }
  77.     public function removeSubscriber(EventSubscriberInterface $subscriber): void
  78.     {
  79.         $this->dispatcher->removeSubscriber($subscriber);
  80.     }
  81.     public function getListeners(?string $eventName null): array
  82.     {
  83.         return $this->dispatcher->getListeners($eventName);
  84.     }
  85.     /**
  86.      * @param callable $listener can not use native type hint as it is incompatible with symfony <5.3.4
  87.      */
  88.     public function getListenerPriority(string $eventName$listener): ?int
  89.     {
  90.         return $this->dispatcher->getListenerPriority($eventName$listener);
  91.     }
  92.     public function hasListeners(?string $eventName null): bool
  93.     {
  94.         return $this->dispatcher->hasListeners($eventName);
  95.     }
  96.     private function getActions(BusinessEventInterface $eventContext $context): EventActionCollection
  97.     {
  98.         $name $event->getName();
  99.         $criteria = new Criteria();
  100.         $criteria->setTitle('business-events::' $event->getName());
  101.         $criteria->addFilter(new EqualsFilter('event_action.eventName'$name));
  102.         $criteria->addFilter(new EqualsFilter('event_action.active'true));
  103.         $criteria->addFilter(new OrFilter([
  104.             new EqualsFilter('event_action.rules.id'null),
  105.             new EqualsAnyFilter('event_action.rules.id'$context->getRuleIds()),
  106.         ]));
  107.         if ($event instanceof SalesChannelAware) {
  108.             $criteria->addFilter(new OrFilter([
  109.                 new EqualsFilter('salesChannels.id'$event->getSalesChannelId()),
  110.                 new EqualsFilter('salesChannels.id'null),
  111.             ]));
  112.         }
  113.         /** @var EventActionCollection $events */
  114.         $events $this->definitionRegistry
  115.             ->getRepository($this->eventActionDefinition->getEntityName())
  116.             ->search($criteria$context)
  117.             ->getEntities();
  118.         return $events;
  119.     }
  120.     private function callActions(BusinessEventInterface $event): void
  121.     {
  122.         $actions $this->getActions($event$event->getContext());
  123.         foreach ($actions as $action) {
  124.             $actionEvent = new BusinessEvent($action->getActionName(), $event$action->getConfig());
  125.             $this->dispatcher->dispatch($actionEvent$actionEvent->getActionName());
  126.         }
  127.         $globalEvent = new BusinessEvent(BusinessEvents::GLOBAL_EVENT$event);
  128.         $this->dispatcher->dispatch($globalEvent$globalEvent->getActionName());
  129.     }
  130. }