custom/plugins/Gartencenter24Theme/src/Subscriber/MailBeforeSentEventSubscriber.php line 52

Open in your IDE?
  1. <?php
  2. namespace Gartencenter24Theme\Subscriber;
  3. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
  4. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Mime\Address;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. class MailBeforeSentEventSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var string|null
  15.      */
  16.     private ?string $customerEmail null;
  17.     /**
  18.      * @var string|null
  19.      */
  20.     private ?string $customerName null;
  21.     /**
  22.      * @var EntityRepositoryInterface
  23.      */
  24.     private $mailTemplateRepository;
  25.     /**
  26.      * @param EntityRepositoryInterface $mailTemplateRepository
  27.      */
  28.     public function __construct(EntityRepositoryInterface $mailTemplateRepository)
  29.     {
  30.         $this->mailTemplateRepository $mailTemplateRepository;
  31.     }
  32.     /**
  33.      * @return string[]
  34.      */
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             MailBeforeValidateEvent::class => 'onMailBeforeValidateEvent',
  39.             MailBeforeSentEvent::class => 'onMailBeforeSentEvent',
  40.         ];
  41.     }
  42.     /**
  43.      * @param MailBeforeValidateEvent $event
  44.      * @return void
  45.      */
  46.     public function onMailBeforeValidateEvent(MailBeforeValidateEvent $event)
  47.     {
  48.         if (
  49.             !array_key_exists('templateId'$event->getData())
  50.             || !$this->checkMailTemplate($event->getData()['templateId'], $event->getContext())
  51.         ) {
  52.             return;
  53.         }
  54.         $this->customerEmail $event->getTemplateData()['contactFormData']['email'];
  55.         $this->customerName $event->getTemplateData()['contactFormData']['firstName'] . ' ' $event->getTemplateData()['contactFormData']['lastName'];
  56.     }
  57.     /**
  58.      * @param MailBeforeSentEvent $event
  59.      * @return void
  60.      */
  61.     public function onMailBeforeSentEvent(MailBeforeSentEvent $event)
  62.     {
  63.         if (
  64.             !array_key_exists('templateId'$event->getData())
  65.             || !$this->checkMailTemplate($event->getData()['templateId'], $event->getContext())
  66.         ) {
  67.             return;
  68.         }
  69.         if ($this->customerEmail === null) {
  70.             return;
  71.         }
  72.         if ($this->customerEmail) {
  73.             $event->getMessage()->replyTo(new Address($this->customerEmail$this->customerName));
  74.         }
  75.     }
  76.     /**
  77.      * @param string $templateId
  78.      * @param Context $context
  79.      * @return bool
  80.      */
  81.     protected function checkMailTemplate (string $templateIdContext $context)
  82.     {
  83.         $criteria = new Criteria();
  84.         $criteria->addAssociation('mailTemplateType');
  85.         $criteria->addFilter(
  86.             new EqualsFilter('id'$templateId),
  87.             new EqualsFilter('mailTemplateType.technicalName''contact_form')
  88.         );
  89.         $template $this->mailTemplateRepository->search($criteria$context);
  90.         if ($template->getTotal() < 1) {
  91.             return false;
  92.         }
  93.         return true;
  94.     }
  95. }