custom/plugins/MoorlForms/src/Core/Subscriber/SendMailSubscriber.php line 104

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlForms\Core\Subscriber;
  3. use MoorlForms\Core\Event\SendMailEvent;
  4. use MoorlForms\Core\Service\FbService;
  5. use Shopware\Core\Checkout\Order\OrderEntity;
  6. use Shopware\Core\Content\Flow\Events\FlowSendMailActionEvent;
  7. use Shopware\Core\Content\Mail\Service\AbstractMailService;
  8. use Shopware\Core\Content\MailTemplate\Event\MailSendSubscriberBridgeEvent;
  9. use Shopware\Core\Content\MailTemplate\Exception\MailEventConfigurationException;
  10. use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
  11. use Shopware\Core\Content\Media\MediaCollection;
  12. use Shopware\Core\Content\Media\MediaService;
  13. use Shopware\Core\Framework\Context;
  14. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  17. use Shopware\Core\Framework\Event\OrderAware;
  18. use Shopware\Core\System\SystemConfig\SystemConfigService;
  19. use Symfony\Contracts\EventDispatcher\Event;
  20. use Shopware\Core\Framework\Event\EventData\EventDataType;
  21. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. class SendMailSubscriber implements EventSubscriberInterface
  24. {
  25.     public const MAIL_TEMPLATE_TYPE 'moorl_fb';
  26.     private AbstractMailService $mailService;
  27.     private EntityRepositoryInterface $mailTemplateRepository;
  28.     private MediaService $mediaService;
  29.     private FbService $fbService;
  30.     private SystemConfigService $systemConfigService;
  31.     public function __construct(
  32.         AbstractMailService $mailService,
  33.         EntityRepositoryInterface $mailTemplateRepository,
  34.         MediaService $mediaService,
  35.         FbService $fbService,
  36.         SystemConfigService $systemConfigService
  37.     )
  38.     {
  39.         $this->mailService $mailService;
  40.         $this->mailTemplateRepository $mailTemplateRepository;
  41.         $this->mediaService $mediaService;
  42.         $this->fbService $fbService;
  43.         $this->systemConfigService $systemConfigService;
  44.     }
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             FlowSendMailActionEvent::class => 'addAttachments',
  49.             MailSendSubscriberBridgeEvent::class => 'addAttachments',
  50.             SendMailEvent::class => 'sendMail',
  51.         ];
  52.     }
  53.     /**
  54.      * Enrich attachments to BusinessEvent or FlowEvent
  55.      *
  56.      * @param $event
  57.      */
  58.     public function addAttachments($event): void
  59.     {
  60.         if ($event instanceof FlowSendMailActionEvent) {
  61.             $mailEvent $event->getFlowEvent()->getEvent();
  62.         } else {
  63.             $mailEvent $event->getBusinessEvent()->getEvent();
  64.         }
  65.         $mediaCollection = new MediaCollection();
  66.         /* Look for attachments in order and line items */
  67.         if ($this->systemConfigService->get('MoorlForms.config.orderSendAttachments')) {
  68.             if ($mailEvent instanceof OrderAware) {
  69.                 /** @var OrderEntity $order */
  70.                 $order $mailEvent->getOrder();
  71.                 $this->fbService->enrichAttachmentsFromEntity($mediaCollection$order$event->getContext());
  72.                 foreach ($order->getLineItems() as $lineItem) {
  73.                     $this->fbService->enrichAttachmentsFromEntity($mediaCollection$lineItem$event->getContext());
  74.                 }
  75.             }
  76.         }
  77.         $attachments $event->getDataBag()->get('binAttachments', []);
  78.         foreach ($mediaCollection as $mediaEntity) {
  79.             $attachments[] = $this->mediaService->getAttachment($mediaEntity$event->getContext());
  80.         }
  81.         $event->getDataBag()->set('binAttachments'$attachments);
  82.     }
  83.     /**
  84.      * Send mail without BusinessEvent or FlowEvent
  85.      *
  86.      * @param SendMailEvent $event
  87.      * @throws MailEventConfigurationException
  88.      */
  89.     public function sendMail(SendMailEvent $event): void
  90.     {
  91.         $form $event->getForm();
  92.         $mailTemplate $this->getMailTemplate($event->getTemplateId(), $event->getContext());
  93.         if (!$form || !$mailTemplate) {
  94.             return;
  95.         }
  96.         $data = new DataBag();
  97.         $data->set('recipients'$event->getMailStruct()->getRecipients());
  98.         if ($event->getReplyTo()) {
  99.             $data->set('replyTo'$event->getReplyTo());
  100.         }
  101.         $data->set('senderName'$mailTemplate->getTranslation('senderName'));
  102.         $data->set('salesChannelId'$event->getSalesChannelId());
  103.         $data->set('templateId'$mailTemplate->getId());
  104.         $data->set('customFields'$mailTemplate->getCustomFields());
  105.         $data->set('contentHtml'$mailTemplate->getTranslation('contentHtml'));
  106.         $data->set('contentPlain'$mailTemplate->getTranslation('contentPlain'));
  107.         $data->set('subject'$mailTemplate->getTranslation('subject'));
  108.         $data->set('mediaIds', []);
  109.         $attachments $form->getAttachments();
  110.         if ($mailTemplate->getMedia()) {
  111.             foreach ($mailTemplate->getMedia() as $mailTemplateMedia) {
  112.                 if (!$mailTemplateMedia->getMedia()) {
  113.                     continue;
  114.                 }
  115.                 if ($mailTemplateMedia->getLanguageId() && $mailTemplateMedia->getLanguageId() !== $event->getContext()->getLanguageId()) {
  116.                     continue;
  117.                 }
  118.                 $attachments[] = $this->mediaService->getAttachment($mailTemplateMedia->getMedia(), $event->getContext());
  119.             }
  120.         }
  121.         if (!empty($attachments)) {
  122.             $data->set('binAttachments'$attachments);
  123.         }
  124.         $this->mailService->send(
  125.             $data->all(),
  126.             $event->getContext(),
  127.             $this->getTemplateData($event)
  128.         );
  129.     }
  130.     private function getMailTemplate(?string $mailTemplateIdContext $context): MailTemplateEntity
  131.     {
  132.         $mailTemplate null;
  133.         if ($mailTemplateId) {
  134.             $criteria = new Criteria([$mailTemplateId]);
  135.             $criteria->addAssociation('media.media');
  136.             $criteria->setLimit(1);
  137.             $mailTemplate $this->mailTemplateRepository->search($criteria$context)->first();
  138.         }
  139.         if (!$mailTemplate) {
  140.             $criteria = new Criteria();
  141.             $criteria->addAssociation('media.media');
  142.             $criteria->setLimit(1);
  143.             $criteria->addFilter(new EqualsFilter('mailTemplateType.technicalName'self::MAIL_TEMPLATE_TYPE));
  144.             $mailTemplate $this->mailTemplateRepository->search($criteria$context)->first();
  145.         }
  146.         return $mailTemplate;
  147.     }
  148.     /**
  149.      * @throws MailEventConfigurationException
  150.      */
  151.     private function getTemplateData(SendMailEvent $event): array
  152.     {
  153.         $data = [];
  154.         /* @var EventDataType $item */
  155.         foreach (array_keys($event::getAvailableData()->toArray()) as $key) {
  156.             $getter 'get' ucfirst($key);
  157.             if (method_exists($event$getter)) {
  158.                 $data[$key] = $event->$getter();
  159.             } else {
  160.                 throw new MailEventConfigurationException('Data for ' $key ' not available.'get_class($event));
  161.             }
  162.         }
  163.         return $data;
  164.     }
  165. }