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

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