vendor/store.shopware.com/viob2bworkflow/src/Core/Framework/Subscriber/BusinessSubscriber.php line 41

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