custom/static-plugins/FroshPlatformTemplateMail/src/Subscriber/FlowSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. namespace Frosh\TemplateMail\Subscriber;
  3. use Frosh\TemplateMail\Event\TemplateMailBusinessEvent;
  4. use Frosh\TemplateMail\Services\MailFinderService;
  5. use Frosh\TemplateMail\Services\MailFinderServiceInterface;
  6. use Shopware\Core\Content\Flow\Events\FlowSendMailActionEvent;
  7. use Shopware\Core\Content\MailTemplate\Aggregate\MailTemplateType\MailTemplateTypeEntity;
  8. use Shopware\Core\Content\MailTemplate\Event\MailSendSubscriberBridgeEvent;
  9. use Shopware\Core\Defaults;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\Event\BusinessEvent;
  14. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\ParameterBag;
  17. class FlowSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var EntityRepository
  21.      */
  22.     private $mailTemplateTypeRepository;
  23.     /**
  24.      * @var MailFinderServiceInterface
  25.      */
  26.     private $mailFinderService;
  27.     public function __construct(
  28.         EntityRepository $mailTemplateTypeRepository,
  29.         MailFinderServiceInterface $mailFinderService
  30.     )
  31.     {
  32.         $this->mailTemplateTypeRepository $mailTemplateTypeRepository;
  33.         $this->mailFinderService $mailFinderService;
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         if (class_exists(FlowSendMailActionEvent::class)) {
  38.             return [
  39.                 FlowSendMailActionEvent::class => 'onFlowSendMailActionEvent'
  40.             ];
  41.         }
  42.         if (class_exists(MailSendSubscriberBridgeEvent::class)) {
  43.             return [
  44.                 MailSendSubscriberBridgeEvent::class => 'onMailSendSubscriberBridgeEvent'
  45.             ];
  46.         }
  47.         return [];
  48.     }
  49.     public function onFlowSendMailActionEvent(FlowSendMailActionEvent $event): void
  50.     {
  51.         $this->sendMail($event->getDataBag(), $event->getMailTemplate()->getMailTemplateTypeId(), $event->getContext());
  52.     }
  53.     public function onMailSendSubscriberBridgeEvent(MailSendSubscriberBridgeEvent $event): void
  54.     {
  55.         $this->sendMail($event->getDataBag(), $event->getMailTemplate()->getMailTemplateTypeId(), $event->getContext());
  56.     }
  57.     public function sendMail(DataBag $dataBagstring $mailTemplateTypeIdContext $context)
  58.     {
  59.         /** @var MailTemplateTypeEntity $mailTemplateType */
  60.         $mailTemplateType $this->mailTemplateTypeRepository->search(new Criteria([$mailTemplateTypeId]), $context) ->first();
  61.         $technicalName $mailTemplateType->getTechnicalName();
  62.         $event $this->createBusinessEventFromBag($dataBag$context);
  63.         $html $this->mailFinderService->findTemplateByTechnicalName(MailFinderService::TYPE_HTML$technicalName$event);
  64.         $plain $this->mailFinderService->findTemplateByTechnicalName(MailFinderService::TYPE_PLAIN$technicalName$event);
  65.         $subject $this->mailFinderService->findTemplateByTechnicalName(MailFinderService::TYPE_SUBJECT$technicalName$event);
  66.         if ($html) {
  67.             $dataBag->set('contentHtml'$html);
  68.         }
  69.         if ($plain) {
  70.             $dataBag->set('contentPlain'$plain);
  71.         }
  72.         if ($subject) {
  73.             $dataBag->set('subject'$subject);
  74.         }
  75.     }
  76.     private function createBusinessEventFromBag(ParameterBag $dataBagContext $context): BusinessEvent
  77.     {
  78.         return new TemplateMailBusinessEvent($dataBag->get('salesChannelId') ?? Defaults::SALES_CHANNEL$context);
  79.     }
  80. }