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

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