custom/plugins/AcrisShopSwitchCS/src/Subscriber/ShopSwitchSubscriber.php line 29

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ShopSwitch\Subscriber;
  3. use Shopware\Core\Framework\Event\BusinessEvent;
  4. use Shopware\Core\Framework\Event\BusinessEventCollectorEvent;
  5. use Shopware\Core\Framework\Event\BusinessEventDefinition;
  6. use Shopware\Core\Framework\Event\BusinessEventInterface;
  7. use Shopware\Core\Framework\Event\MailActionInterface;
  8. use Shopware\Core\Framework\Event\SalesChannelAware;
  9. use Shopware\Core\Framework\Log\LogAwareBusinessEventInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ShopSwitchSubscriber implements EventSubscriberInterface
  12. {
  13.     public const CUSTOM_EVENTS_CLASSES = [
  14.         'Acris\ShopSwitch\Core\Content\ShopSwitch\Event\ShopSwitchDatabaseUpdateEvent'
  15.     ];
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             BusinessEventCollectorEvent::NAME => [
  20.                 ['onBusinessEventCollect'200]
  21.             ]
  22.         ];
  23.     }
  24.     public function onBusinessEventCollect(BusinessEventCollectorEvent $event): void
  25.     {
  26.         $result $event->getCollection();
  27.         foreach (self::CUSTOM_EVENTS_CLASSES as $class) {
  28.             $definition $this->define($class);
  29.             if (!$definition) {
  30.                 continue;
  31.             }
  32.             $result->set($definition->getName(), $definition);
  33.         }
  34.     }
  35.     private function define(string $class, ?string $name null): ?BusinessEventDefinition
  36.     {
  37.         if ($class === BusinessEvent::class) {
  38.             return null;
  39.         }
  40.         $instance = (new \ReflectionClass($class))
  41.             ->newInstanceWithoutConstructor();
  42.         if (!$instance instanceof BusinessEventInterface) {
  43.             throw new \RuntimeException(sprintf('Event %s is not a business event'$class));
  44.         }
  45.         $name $name ?? $instance->getName();
  46.         if (!$name) {
  47.             return null;
  48.         }
  49.         return new BusinessEventDefinition(
  50.             $name,
  51.             $class,
  52.             $instance instanceof MailActionInterface,
  53.             $instance instanceof LogAwareBusinessEventInterface,
  54.             $instance instanceof SalesChannelAware,
  55.             $instance::getAvailableData()->toArray()
  56.         );
  57.     }
  58. }