custom/plugins/VioRepresentativeLogin/src/Core/Framework/Subscriber/BusinessSubscriber.php line 55

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace VioRepresentativeLogin\Core\Framework\Subscriber;
  4. use ReflectionClass;
  5. use ReflectionException;
  6. use RuntimeException;
  7. use Shopware\Core\Framework\Event\BusinessEventCollectorEvent;
  8. use Shopware\Core\Framework\Event\BusinessEventDefinition;
  9. use Shopware\Core\Framework\Event\BusinessEventInterface;
  10. use Shopware\Core\Framework\Event\FlowEventAware;
  11. use Shopware\Core\Framework\Event\MailActionInterface;
  12. use Shopware\Core\Framework\Event\SalesChannelAware;
  13. use Shopware\Core\Framework\Feature;
  14. use Shopware\Core\Framework\Log\LogAwareBusinessEventInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use VioB2BLogin\Core\Checkout\Cart\Event\CheckoutEmployeeOrderPlacedEvent;
  17. use VioB2BLogin\Core\Checkout\Customer\Event\EmployeeBeforeLoginEvent;
  18. use VioB2BLogin\Core\Checkout\Customer\Event\EmployeeCreateEvent;
  19. use VioB2BLogin\Core\Checkout\Customer\Event\EmployeeDeletedEvent;
  20. use VioB2BLogin\Core\Checkout\Customer\Event\EmployeeLoginEvent;
  21. use VioB2BLogin\Core\Checkout\Customer\Event\EmployeeUpdateEvent;
  22. use VioRepresentativeLogin\Core\Checkout\Cart\Event\CheckoutAgentOrderPlacedEvent;
  23. use VioRepresentativeLogin\Core\Checkout\Customer\Event\AgentBeforeLoginEvent;
  24. use VioRepresentativeLogin\Core\Checkout\Customer\Event\AgentCustomerLogoutEvent;
  25. use VioRepresentativeLogin\Core\Checkout\Customer\Event\AgentLoginEvent;
  26. use VioRepresentativeLogin\Core\Checkout\Customer\Event\AgentLogoutEvent;
  27. class BusinessSubscriber implements EventSubscriberInterface
  28. {
  29.     public const EVENTS = [
  30.         CheckoutAgentOrderPlacedEvent::class,
  31.         AgentBeforeLoginEvent::class,
  32.         AgentLoginEvent::class,
  33.         AgentLogoutEvent::class,
  34.         AgentCustomerLogoutEvent::class
  35.     ];
  36.     /**
  37.      * @inheritDoc
  38.      */
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             BusinessEventCollectorEvent::NAME => 'onCollectBusinessEvents'
  43.         ];
  44.     }
  45.     /**
  46.      * @throws ReflectionException
  47.      */
  48.     public function onCollectBusinessEvents(BusinessEventCollectorEvent $event): void
  49.     {
  50.         // add BusinessEvents
  51.         foreach (self::EVENTS as $eventClassName) {
  52.             $definition $this->createDefinition($eventClassName);
  53.             $event->getCollection()->set($definition->getName(), $definition);
  54.         }
  55.     }
  56.     /**
  57.      * @throws ReflectionException
  58.      */
  59.     private function createDefinition(string $eventClassName): BusinessEventDefinition
  60.     {
  61.         $reflectionClass = new ReflectionClass($eventClassName);
  62.         if (Feature::isActive('FEATURE_NEXT_17858')) {
  63.             if (!$reflectionClass->implementsInterface(FlowEventAware::class)) {
  64.                 throw new RuntimeException(sprintf('Event %s is not a business event'$eventClassName));
  65.             }
  66.         } else if (!$reflectionClass->implementsInterface(BusinessEventInterface::class)) {
  67.             throw new RuntimeException(sprintf('Event %s is not a business event'$eventClassName));
  68.         }
  69.         $instance = (new ReflectionClass($eventClassName))->newInstanceWithoutConstructor();//NOSONAR
  70.         $name $instance->getName();
  71.         $aware array_keys(array_filter($reflectionClass->getInterfaces(), static function (ReflectionClass $interface) {
  72.                 return $interface->isSubclassOf(FlowEventAware::class);
  73.             })
  74.         );
  75.         return new BusinessEventDefinition(
  76.             $name,
  77.             $eventClassName,
  78.             $instance instanceof MailActionInterface,
  79.             $instance instanceof LogAwareBusinessEventInterface,
  80.             $instance instanceof SalesChannelAware,
  81.             $instance::getAvailableData()->toArray(),
  82.             $aware
  83.         );
  84.     }
  85. }