custom/plugins/NetiNextStoreLocator/src/Subscriber/BusinessEventSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextStoreLocator\Subscriber;
  4. use NetInventors\NetiNextStoreLocator\Components\Event\ContactEvent;
  5. use NetInventors\NetiNextStoreLocator\Constants\BusinessEventsConstants;
  6. use NetInventors\NetiNextStoreLocator\Constants\FlowConstants;
  7. use Shopware\Core\Content\Flow\Events\FlowSendMailActionEvent;
  8. use Shopware\Core\Content\MailTemplate\Event\MailSendSubscriberBridgeEvent;
  9. use Shopware\Core\Framework\Event\BusinessEventCollector;
  10. use Shopware\Core\Framework\Event\BusinessEventCollectorEvent;
  11. use Shopware\Core\Framework\Event\BusinessEventDefinition;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\File\UploadedFile;
  14. class BusinessEventSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var BusinessEventCollector
  18.      */
  19.     private        $businessEventCollector;
  20.     private string $version;
  21.     public function __construct(
  22.         BusinessEventCollector $businessEventCollector,
  23.         string                 $version
  24.     ) {
  25.         $this->businessEventCollector $businessEventCollector;
  26.         $this->version                $version;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         /**
  31.          * The MailSendSubscriberBridgeEvent class is deprecated but required to make the feature work.
  32.          *
  33.          * @psalm-suppress DeprecatedClass
  34.          */
  35.         return [
  36.             BusinessEventCollectorEvent::NAME    => 'onCollectBusinessEvents',
  37.             MailSendSubscriberBridgeEvent::class => 'onMailSend',
  38.             FlowSendMailActionEvent::class       => 'onSendMailAction',
  39.         ];
  40.     }
  41.     public function onCollectBusinessEvents(BusinessEventCollectorEvent $event): void
  42.     {
  43.         /** @psalm-suppress DeprecatedClass */
  44.         $definitionClasses BusinessEventsConstants::EVENT_CLASSES;
  45.         if (\version_compare($this->version'6.4.6.0''>=')) {
  46.             $definitionClasses FlowConstants::EVENT_CLASSES;
  47.         }
  48.         foreach ($definitionClasses as $class) {
  49.             $eventDefinition $this->businessEventCollector->define($class);
  50.             if ($eventDefinition instanceof BusinessEventDefinition) {
  51.                 $event->getCollection()->set($class$eventDefinition);
  52.             }
  53.         }
  54.     }
  55.     /**
  56.      * The MailSendSubscriberBridgeEvent class is deprecated but required to make the feature work.
  57.      *
  58.      * @psalm-suppress DeprecatedClass
  59.      */
  60.     public function onMailSend(MailSendSubscriberBridgeEvent $event): void
  61.     {
  62.         /**
  63.          * The returned BusinessEvent class is deprecated but required to make the feature work.
  64.          *
  65.          * @psalm-suppress DeprecatedClass
  66.          */
  67.         $businessEvent $event->getBusinessEvent()->getEvent();
  68.         if (!$businessEvent instanceof ContactEvent) {
  69.             return;
  70.         }
  71.         $event->getDataBag()->set('binAttachments'$this->getAttachments($businessEvent));
  72.     }
  73.     public function onSendMailAction(FlowSendMailActionEvent $event): void
  74.     {
  75.         /**
  76.          * @psalm-suppress DeprecatedClass  The FlowEvent class will be deprecated with 6.5.x
  77.          * @psalm-suppress DeprecatedMethod The getFlowEvent method will be deprecated with 6.5.x
  78.          */
  79.         $flowEvent $event->getFlowEvent()->getEvent();
  80.         if (!$flowEvent instanceof \NetInventors\NetiNextStoreLocator\Components\FlowEvent\ContactEvent) {
  81.             return;
  82.         }
  83.         $event->getDataBag()->set('binAttachments'$this->getAttachments($flowEvent));
  84.     }
  85.     private function getAttachments(ContactEvent $event): array
  86.     {
  87.         $attachments = [];
  88.         /** @var array<string, mixed> $file */
  89.         foreach ($event->getFiles() as $file) {
  90.             /** @var UploadedFile $uploadedFile */
  91.             $uploadedFile $file['file'];
  92.             /** @var string $name */
  93.             $name $file['name'];
  94.             $attachments[] = [
  95.                 'content'  => $uploadedFile->getContent(),
  96.                 'fileName' => $name,
  97.                 'mimeType' => $uploadedFile->getMimeType(),
  98.             ];
  99.         }
  100.         return $attachments;
  101.     }
  102. }