vendor/shopware/core/Content/MailTemplate/Subscriber/MailSendSubscriber.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\MailTemplate\Subscriber;
  3. use Shopware\Core\Content\MailTemplate\Exception\MailEventConfigurationException;
  4. use Shopware\Core\Content\MailTemplate\Exception\SalesChannelNotFoundException;
  5. use Shopware\Core\Content\MailTemplate\MailTemplateActions;
  6. use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
  7. use Shopware\Core\Content\MailTemplate\Service\MailServiceInterface;
  8. use Shopware\Core\Content\Media\MediaService;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\Event\BusinessEvent;
  14. use Shopware\Core\Framework\Event\EventData\EventDataType;
  15. use Shopware\Core\Framework\Event\MailActionInterface;
  16. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class MailSendSubscriber implements EventSubscriberInterface
  19. {
  20.     public const ACTION_NAME MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION;
  21.     /**
  22.      * @var MailServiceInterface
  23.      */
  24.     private $mailService;
  25.     /**
  26.      * @var EntityRepositoryInterface
  27.      */
  28.     private $mailTemplateRepository;
  29.     /**
  30.      * @var MediaService
  31.      */
  32.     private $mediaService;
  33.     public function __construct(
  34.         MailServiceInterface $mailService,
  35.         EntityRepositoryInterface $mailTemplateRepository,
  36.         MediaService $mediaService
  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.         if (!$mailEvent instanceof MailActionInterface) {
  57.             throw new MailEventConfigurationException('Not a instance of MailActionInterface'get_class($mailEvent));
  58.         }
  59.         if (!\array_key_exists('mail_template_type_id'$event->getConfig())) {
  60.             throw new MailEventConfigurationException('Configuration mail_template_type_id missing.'get_class($mailEvent));
  61.         }
  62.         $mailTemplateTypeId $event->getConfig()['mail_template_type_id'];
  63.         $criteria = new Criteria();
  64.         $criteria->addFilter(new EqualsFilter('mailTemplateTypeId'$mailTemplateTypeId));
  65.         $criteria->addAssociation('media.media');
  66.         $criteria->setLimit(1);
  67.         if ($mailEvent->getSalesChannelId()) {
  68.             $criteria->addFilter(new EqualsFilter('mail_template.salesChannels.salesChannel.id'$mailEvent->getSalesChannelId()));
  69.             /** @var MailTemplateEntity|null $mailTemplate */
  70.             $mailTemplate $this->mailTemplateRepository->search($criteria$event->getContext())->first();
  71.             // Fallback if no template for the saleschannel is found
  72.             if ($mailTemplate === null) {
  73.                 $criteria = new Criteria();
  74.                 $criteria->addFilter(new EqualsFilter('mailTemplateTypeId'$mailTemplateTypeId));
  75.                 $criteria->addAssociation('media.media');
  76.                 $criteria->setLimit(1);
  77.                 /** @var MailTemplateEntity|null $mailTemplate */
  78.                 $mailTemplate $this->mailTemplateRepository->search($criteria$event->getContext())->first();
  79.             }
  80.         } else {
  81.             /** @var MailTemplateEntity|null $mailTemplate */
  82.             $mailTemplate $this->mailTemplateRepository->search($criteria$event->getContext())->first();
  83.         }
  84.         if ($mailTemplate === null) {
  85.             return;
  86.         }
  87.         $data = new DataBag();
  88.         $data->set('recipients'$mailEvent->getMailStruct()->getRecipients());
  89.         $data->set('senderName'$mailTemplate->getTranslation('senderName'));
  90.         $data->set('salesChannelId'$mailEvent->getSalesChannelId());
  91.         $data->set('templateId'$mailTemplate->getId());
  92.         $data->set('customFields'$mailTemplate->getCustomFields());
  93.         $data->set('contentHtml'$mailTemplate->getTranslation('contentHtml'));
  94.         $data->set('contentPlain'$mailTemplate->getTranslation('contentPlain'));
  95.         $data->set('subject'$mailTemplate->getTranslation('subject'));
  96.         $data->set('mediaIds', []);
  97.         $attachments = [];
  98.         if ($mailTemplate->getMedia() !== null) {
  99.             foreach ($mailTemplate->getMedia() as $mailTemplateMedia) {
  100.                 if ($mailTemplateMedia->getMedia() === null) {
  101.                     continue;
  102.                 }
  103.                 if ($mailTemplateMedia->getLanguageId() !== null && $mailTemplateMedia->getLanguageId() !== $event->getContext()
  104.                         ->getLanguageId()) {
  105.                     continue;
  106.                 }
  107.                 $attachments[] = $this->mediaService->getAttachment(
  108.                     $mailTemplateMedia->getMedia(),
  109.                     $event->getContext()
  110.                 );
  111.             }
  112.         }
  113.         if (!empty($attachments)) {
  114.             $data->set('binAttachments'$attachments);
  115.         }
  116.         $this->mailService->send(
  117.             $data->all(),
  118.             $event->getContext(),
  119.             $this->getTemplateData($mailEvent)
  120.         );
  121.     }
  122.     /**
  123.      * @throws MailEventConfigurationException
  124.      */
  125.     private function getTemplateData(MailActionInterface $event): array
  126.     {
  127.         $data = [];
  128.         /* @var EventDataType $item */
  129.         foreach (array_keys($event::getAvailableData()->toArray()) as $key) {
  130.             $getter 'get' ucfirst($key);
  131.             if (method_exists($event$getter)) {
  132.                 $data[$key] = $event->$getter();
  133.             } else {
  134.                 throw new MailEventConfigurationException('Data for ' $key ' not available.'get_class($event));
  135.             }
  136.         }
  137.         return $data;
  138.     }
  139. }