custom/plugins/VioB2BWorkflow/src/Core/Framework/Subscriber/BusinessSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Vio\B2BWorkflow\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\Log\LogAwareBusinessEventInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Vio\B2BWorkflow\Core\Checkout\Cart\Event\CheckoutCustomerOrderRequestPlacedEvent;
  16. use Vio\B2BWorkflow\Core\Checkout\Cart\Event\CheckoutEmployeeOrderRequestPlacedEvent;
  17. class BusinessSubscriber implements EventSubscriberInterface
  18. {
  19.     public const EVENTS = [
  20.         CheckoutEmployeeOrderRequestPlacedEvent::class,
  21.         CheckoutCustomerOrderRequestPlacedEvent::class
  22.     ];
  23.     /**
  24.      * @inheritDoc
  25.      */
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             BusinessEventCollectorEvent::NAME => 'onCollectBusinessEvents'
  30.         ];
  31.     }
  32.     /**
  33.      * @throws ReflectionException
  34.      */
  35.     public function onCollectBusinessEvents(BusinessEventCollectorEvent $event): void
  36.     {
  37.         // add BusinessEvents
  38.         foreach (self::EVENTS as $eventClassName) {
  39.             $definition $this->createDefinition($eventClassName);
  40.             $event->getCollection()->set($definition->getName(), $definition);
  41.         }
  42.     }
  43.     /**
  44.      * @throws ReflectionException
  45.      */
  46.     private function createDefinition(string $eventClassName): BusinessEventDefinition
  47.     {
  48.         $reflectionClass = new ReflectionClass($eventClassName);
  49.         if (!$reflectionClass->implementsInterface(BusinessEventInterface::class)) {
  50.             throw new RuntimeException(sprintf('Event %s is not a business event'$eventClassName));
  51.         }
  52.         $aware array_keys(array_filter($reflectionClass->getInterfaces(), static function (ReflectionClass $interface) {
  53.                 return $interface->isSubclassOf(FlowEventAware::class);
  54.             })
  55.         );
  56.         $instance $reflectionClass->newInstanceWithoutConstructor();//NOSONAR
  57.         $name $instance->getName();
  58.         return new BusinessEventDefinition(
  59.             $name,
  60.             $eventClassName,
  61.             $instance instanceof MailActionInterface,
  62.             $instance instanceof LogAwareBusinessEventInterface,
  63.             $instance instanceof SalesChannelAware,
  64.             $instance::getAvailableData()->toArray(),
  65.             $aware
  66.         );
  67.     }
  68. }