custom/plugins/AcrisMailerSalesChannelCS/src/Core/Content/Mail/Subscriber/MailSendSubscriber.php line 47

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\MailerSalesChannel\Core\Content\Mail\Subscriber;
  3. use Acris\MailerSalesChannel\Core\Content\Mail\Acris_Esmtp\Acris_EsmtpTransport;
  4. use Acris\MailerSalesChannel\Core\Content\Mail\Service\Mailer;
  5. use Acris\MailerSalesChannel\Core\Content\Mail\Service\Transport\Transports;
  6. use Shopware\Core\Content\MailTemplate\Exception\MailEventConfigurationException;
  7. use Shopware\Core\Content\MailTemplate\Exception\SalesChannelNotFoundException;
  8. use Shopware\Core\Content\MailTemplate\MailTemplateActions;
  9. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  11. use Shopware\Core\Framework\Event\FlowEvent;
  12. use Shopware\Core\Framework\Event\MailActionInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class MailSendSubscriber implements EventSubscriberInterface
  15. {
  16.     public const ACTION_NAME MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION;
  17.     /**
  18.      * @var Mailer
  19.      */
  20.     private $mailer;
  21.     public function __construct(
  22.         Mailer $mailer
  23.     ) {
  24.         $this->mailer $mailer;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             self::ACTION_NAME => [
  30.                 ['sendMail'1000]
  31.             ],
  32.             MailBeforeSentEvent::class => 'beforeSendMail'
  33.         ];
  34.     }
  35.     /**
  36.      * @throws MailEventConfigurationException
  37.      * @throws SalesChannelNotFoundException
  38.      * @throws InconsistentCriteriaIdsException
  39.      */
  40.     public function sendMail($event): void
  41.     {
  42.         if ($event instanceof FlowEvent) {
  43.             $mailEvent $event->getEvent();
  44.             if (!$mailEvent instanceof MailActionInterface) {
  45.                 throw new MailEventConfigurationException('Not a instance of MailActionInterface'get_class($mailEvent));
  46.             }
  47.             if (empty($mailEvent->getSalesChannelId()) === false && $this->mailer instanceof Mailer && $this->mailer->getTransport() instanceof Transports) {
  48.                 foreach ($this->mailer->getTransport()->getTransports() as $transport) {
  49.                     if ($transport instanceof Acris_EsmtpTransport) {
  50.                         $transport->recreateFromSwConfig($mailEvent->getSalesChannelId());
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.     }
  56.     /**
  57.      * Event needed if mail is sent from state change from admin or api
  58.      * @param MailBeforeSentEvent $event
  59.      */
  60.     public function beforeSendMail(MailBeforeSentEvent $event)
  61.     {
  62.         $data $event->getData();
  63.         if(array_key_exists('salesChannelId'$data) && $data['salesChannelId'] && $this->mailer instanceof Mailer && $this->mailer->getTransport() instanceof Transports) {
  64.             foreach ($this->mailer->getTransport()->getTransports() as $transport) {
  65.                 if ($transport instanceof Acris_EsmtpTransport && (empty($transport->getSalesChannelId()) === true || $transport->getSalesChannelId() !== $data['salesChannelId'])) {
  66.                     $transport->recreateFromSwConfig($data['salesChannelId']);
  67.                 }
  68.             }
  69.         }
  70.     }
  71. }