custom/plugins/MoorlCustomerAccounts/src/Subscriber/MailSendSubscriber.php line 30

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlCustomerAccounts\Subscriber;
  3. use MoorlCustomerAccounts\Core\Service\CustomerAccountService;
  4. use Shopware\Core\Content\Flow\Events\FlowSendMailActionEvent;
  5. use Shopware\Core\Content\MailTemplate\Event\MailSendSubscriberBridgeEvent;
  6. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class MailSendSubscriber implements EventSubscriberInterface
  9. {
  10.     private CustomerAccountService $customerAccountService;
  11.     public function __construct(
  12.         CustomerAccountService $customerAccountService
  13.     )
  14.     {
  15.         $this->customerAccountService $customerAccountService;
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             MailSendSubscriberBridgeEvent::class => 'onMailSendSubscriberBridge',
  21.             FlowSendMailActionEvent::class => 'onFlowSendMailActionEvent',
  22.         ];
  23.     }
  24.     public function onFlowSendMailActionEvent(FlowSendMailActionEvent $event): void
  25.     {
  26.         $orderFlows $this->customerAccountService->getOrderFlows();
  27.         foreach ($orderFlows as $orderFlow) {
  28.             if ($event->getFlowEvent()->getEvent()->getName() === $orderFlow->getEventName()) {
  29.                 try {
  30.                     $eventName str_replace(".""_"$orderFlow->getEventName());
  31.                     $customer $event->getFlowEvent()->getEvent()->getOrder()->getOrderCustomer()->getCustomer();
  32.                     $emailAddresses $customer->getCustomFields()['moorl_ca_email'][$eventName];
  33.                     if (empty($emailAddresses)) {
  34.                         return;
  35.                     }
  36.                 } catch (\Exception $exception) {
  37.                     return;
  38.                 }
  39.                 $emailAddresses explode(";"$emailAddresses);
  40.                 $emailAddresses array_map('trim'$emailAddresses);
  41.                 $recipients = [];
  42.                 foreach ($emailAddresses as $emailAddress) {
  43.                     $recipients[$emailAddress] = $emailAddress;
  44.                 }
  45.                 $event->getDataBag()->set('recipients'$recipients);
  46.             }
  47.         }
  48.     }
  49.     public function onMailSendSubscriberBridge(MailSendSubscriberBridgeEvent $event): void
  50.     {
  51.         $orderFlows $this->customerAccountService->getOrderBusinessEvents();
  52.         foreach ($orderFlows as $orderFlow) {
  53.             if ($event->getBusinessEvent()->getEvent()->getName() === $orderFlow->getEventName()) {
  54.                 try {
  55.                     $eventName str_replace(".""_"$orderFlow->getEventName());
  56.                     $customer $event->getBusinessEvent()->getEvent()->getOrder()->getOrderCustomer()->getCustomer();
  57.                     $emailAddresses $customer->getCustomFields()['moorl_ca_email'][$eventName];
  58.                     if (empty($emailAddresses)) {
  59.                         return;
  60.                     }
  61.                 } catch (\Exception $exception) {
  62.                     return;
  63.                 }
  64.                 $emailAddresses explode(";"$emailAddresses);
  65.                 $emailAddresses array_map('trim'$emailAddresses);
  66.                 $recipients = [];
  67.                 foreach ($emailAddresses as $emailAddress) {
  68.                     $recipients[$emailAddress] = $emailAddress;
  69.                 }
  70.                 $event->getDataBag()->set('recipients'$recipients);
  71.             }
  72.         }
  73.     }
  74. }