vendor/store.shopware.com/viob2blogin/src/Core/Framework/Subscriber/BusinessSubscriber.php line 53

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