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

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