custom/plugins/MoorlFormBuilder/src/Subscriber/MailSendSubscriber.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlFormBuilder\Subscriber;
  3. use MoorlFormBuilder\MoorlFormBuilder;
  4. use Shopware\Core\Content\MailTemplate\Exception\MailEventConfigurationException;
  5. use Shopware\Core\Content\MailTemplate\Exception\SalesChannelNotFoundException;
  6. use Shopware\Core\Content\MailTemplate\Service\MailServiceInterface;
  7. use Shopware\Core\Content\Media\MediaService;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\Event\BusinessEvent;
  13. use Shopware\Core\Framework\Event\EventData\EventDataType;
  14. use Shopware\Core\Framework\Event\MailActionInterface;
  15. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class MailSendSubscriber implements EventSubscriberInterface
  18. {
  19.     public const ACTION_NAME MoorlFormBuilder::MAIL_TEMPLATE_MAIL_SEND_ACTION;
  20.     /**
  21.      * @var MailServiceInterface
  22.      */
  23.     private $mailService;
  24.     /**
  25.      * @var EntityRepositoryInterface
  26.      */
  27.     private $mailTemplateRepository;
  28.     /**
  29.      * @var MediaService
  30.      */
  31.     private $mediaService;
  32.     public function __construct(
  33.         MailServiceInterface $mailService,
  34.         EntityRepositoryInterface $mailTemplateRepository,
  35.         MediaService $mediaService
  36.     )
  37.     {
  38.         $this->mailService $mailService;
  39.         $this->mailTemplateRepository $mailTemplateRepository;
  40.         $this->mediaService $mediaService;
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             self::ACTION_NAME => 'sendMail',
  46.         ];
  47.     }
  48.     /**
  49.      * @throws MailEventConfigurationException
  50.      * @throws SalesChannelNotFoundException
  51.      * @throws InconsistentCriteriaIdsException
  52.      */
  53.     public function sendMail(BusinessEvent $event): void
  54.     {
  55.         $mailEvent $event->getEvent();
  56.         $form $mailEvent->getForm();
  57.         if (!$form) {
  58.             return;
  59.         }
  60.         if (!$mailEvent instanceof MailActionInterface) {
  61.             throw new MailEventConfigurationException('Not a instance of MailActionInterface'get_class($mailEvent));
  62.         }
  63.         if (!\array_key_exists('mail_template_type_id'$event->getConfig())) {
  64.             throw new MailEventConfigurationException('Configuration mail_template_type_id missing.'get_class($mailEvent));
  65.         }
  66.         $mailTemplateTypeId $event->getConfig()['mail_template_type_id'];
  67.         $mailTemplateId $form->getMailTemplateId();
  68.         $mailTemplate null;
  69.         if ($mailTemplateId) {
  70.             $criteria = new Criteria([$form->getMailTemplateId()]);
  71.             $criteria->addAssociation('media.media');
  72.             $criteria->setLimit(1);
  73.             $mailTemplate $this->mailTemplateRepository->search($criteria$event->getContext())->first();
  74.         }
  75.         if (!$mailTemplate) {
  76.             $criteria = new Criteria();
  77.             $criteria->addFilter(new EqualsFilter('mailTemplateTypeId'$mailTemplateTypeId));
  78.             $criteria->addAssociation('media.media');
  79.             $criteria->setLimit(1);
  80.             $mailTemplate $this->mailTemplateRepository->search($criteria$event->getContext())->first();
  81.         }
  82.         $data = new DataBag();
  83.         $data->set('recipients'$mailEvent->getMailStruct()->getRecipients());
  84.         if ($form->getReplyTo()) {
  85.             $data->set('replyTo'$form->getReplyTo());
  86.         }
  87.         $data->set('senderName'$mailTemplate->getTranslation('senderName'));
  88.         $data->set('salesChannelId'$mailEvent->getSalesChannelId());
  89.         $data->set('templateId'$mailTemplate->getId());
  90.         $data->set('customFields'$mailTemplate->getCustomFields());
  91.         $data->set('contentHtml'$mailTemplate->getTranslation('contentHtml'));
  92.         $data->set('contentPlain'$mailTemplate->getTranslation('contentPlain'));
  93.         $data->set('subject'$mailTemplate->getTranslation('subject'));
  94.         $data->set('mediaIds', []);
  95.         $attachments = [];
  96.         if ($mailEvent->getMedias()) {
  97.             foreach ($mailEvent->getMedias() as $mailEventMedia) {
  98.                 if (!$mailEventMedia) {
  99.                     continue;
  100.                 }
  101.                 $fileName mb_substr($mailEventMedia->getFileName(), 33);
  102.                 $updatedMedia = clone $mailEventMedia;
  103.                 $updatedMedia->setFileName($fileName);
  104.                 $attachments[] = $this->mediaService->getAttachment(
  105.                     $updatedMedia,
  106.                     $event->getContext()
  107.                 );
  108.             }
  109.         }
  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(
  119.                     $mailTemplateMedia->getMedia(),
  120.                     $event->getContext()
  121.                 );
  122.             }
  123.         }
  124.         if (!empty($attachments)) {
  125.             $data->set('binAttachments'$attachments);
  126.         }
  127.         $this->mailService->send(
  128.             $data->all(),
  129.             $event->getContext(),
  130.             $this->getTemplateData($mailEvent)
  131.         );
  132.     }
  133.     /**
  134.      * @throws MailEventConfigurationException
  135.      */
  136.     private function getTemplateData(MailActionInterface $event): array
  137.     {
  138.         $data = [];
  139.         /* @var EventDataType $item */
  140.         foreach (array_keys($event::getAvailableData()->toArray()) as $key) {
  141.             $getter 'get' ucfirst($key);
  142.             if (method_exists($event$getter)) {
  143.                 $data[$key] = $event->$getter();
  144.             } else {
  145.                 throw new MailEventConfigurationException('Data for ' $key ' not available.'get_class($event));
  146.             }
  147.         }
  148.         return $data;
  149.     }
  150. }