custom/plugins/NewsletterSendinblue/src/Subscriber/NewsletterRecipientSubscriber.php line 136

Open in your IDE?
  1. <?php
  2. namespace NewsletterSendinblue\Subscriber;
  3. use NewsletterSendinblue\Service\ConfigService;
  4. use NewsletterSendinblue\Service\NewsletterRecipient\NewsletterRecipientProducer;
  5. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  6. use Shopware\Core\Content\Newsletter\Event\NewsletterConfirmEvent;
  7. use Shopware\Core\Content\Newsletter\Event\NewsletterRegisterEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenContainerEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. class NewsletterRecipientSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var NewsletterRecipientProducer
  19.      */
  20.     private $newsletterRecipientProducer;
  21.     /**
  22.      * @var ConfigService
  23.      */
  24.     private $configService;
  25.     /**
  26.      * @var SystemConfigService
  27.      */
  28.     private $systemConfigService;
  29.     /**
  30.      * @var EntityRepositoryInterface
  31.      */
  32.     private $mailTemplateRepository;
  33.     /**
  34.      * @var RequestStack;
  35.      */
  36.     private $requestStack;
  37.     /**
  38.      * @param NewsletterRecipientProducer $newsletterRecipientProducer
  39.      * @param ConfigService $configService
  40.      * @param SystemConfigService $systemConfigService
  41.      * @param EntityRepositoryInterface $mailTemplateRepository
  42.      * @param RequestStack $requestStack
  43.      */
  44.     public function __construct(
  45.         NewsletterRecipientProducer $newsletterRecipientProducer,
  46.         ConfigService               $configService,
  47.         SystemConfigService         $systemConfigService,
  48.         EntityRepositoryInterface   $mailTemplateRepository,
  49.         RequestStack                $requestStack
  50.     )
  51.     {
  52.         $this->newsletterRecipientProducer $newsletterRecipientProducer;
  53.         $this->configService $configService;
  54.         $this->systemConfigService $systemConfigService;
  55.         $this->mailTemplateRepository $mailTemplateRepository;
  56.         $this->requestStack $requestStack;
  57.     }
  58.     /**
  59.      * @return array
  60.      */
  61.     public static function getSubscribedEvents(): array
  62.     {
  63.         return [
  64.             NewsletterConfirmEvent::class => 'onNewsletterConfirmEvent',
  65.             NewsletterRegisterEvent::class => 'onNewsletterRegisterEvent',
  66.             EntityWrittenContainerEvent::class => 'onEntityWrittenContainerEvent',
  67.             MailBeforeValidateEvent::class => 'onMailBeforeValidateEvent',
  68.         ];
  69.     }
  70.     /**
  71.      * @param NewsletterConfirmEvent $event
  72.      */
  73.     public function onNewsletterConfirmEvent(NewsletterConfirmEvent $event): void
  74.     {
  75.         $this->newsletterRecipientProducer->confirmContact($event->getNewsletterRecipient(), $event->getSalesChannelId());
  76.     }
  77.     /**
  78.      * @param NewsletterRegisterEvent $event
  79.      */
  80.     public function onNewsletterRegisterEvent(NewsletterRegisterEvent $event): void
  81.     {
  82.         $this->configService->setSalesChannelId($event->getSalesChannelId());
  83.         if ($this->configService->getSubscriptionMailing()) {
  84.             $this->newsletterRecipientProducer->confirmContact($event->getNewsletterRecipient(), $event->getSalesChannelId());
  85.             if ($this->requestStack
  86.                 && $this->requestStack->getCurrentRequest()
  87.                 && $this->requestStack->getCurrentRequest()->hasSession()
  88.                 && $this->requestStack->getSession()->has('sbCoreDoubleOptIn')
  89.             ) {
  90.                 $session $this->requestStack->getSession();
  91.                 $this->systemConfigService->set('core.newsletter.doubleOptIn'$session->get('sbCoreDoubleOptIn'));
  92.                 $session->remove('sbCoreDoubleOptIn');
  93.             }
  94.         }
  95.     }
  96.     /**
  97.      * @param EntityWrittenContainerEvent $event
  98.      */
  99.     public function onEntityWrittenContainerEvent(EntityWrittenContainerEvent $event): void
  100.     {
  101.         foreach ($event->getEvents()->getElements() as $eventElement) {
  102.             if ($eventElement->getName() === 'newsletter_recipient.written') {
  103.                 foreach ($eventElement->getWriteResults() as $writeResult) {
  104.                     if ($writeResult->getOperation() === 'update') {
  105.                         $payload $writeResult->getPayload();
  106.                         $newsletterRecipientId $writeResult->getPrimaryKey();
  107.                         $salesChannelId $payload['salesChannelId'] ?? null;
  108.                         if (isset($payload['status']) && $payload['status'] == 'optOut') {
  109.                             $this->newsletterRecipientProducer->unsubscribeContact($newsletterRecipientId$salesChannelId);
  110.                         } else {
  111.                             $this->newsletterRecipientProducer->updateContact($newsletterRecipientId$salesChannelId);
  112.                         }
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.     }
  118.     /**
  119.      * @param MailBeforeValidateEvent $event
  120.      */
  121.     public function onMailBeforeValidateEvent(MailBeforeValidateEvent $event): void
  122.     {
  123.         $data $event->getData();
  124.         $templateData $event->getTemplateData();
  125.         $context $event->getContext();
  126.         if (!isset($templateData['newsletterRecipient']) || !isset($data['templateId'])) {
  127.             return;
  128.         }
  129.         if (!$this->isNewsletterMailTemplate($data['templateId'], $context)) {
  130.             return;
  131.         }
  132.         $salesChannelId null;
  133.         if (!empty($data['salesChannelId'])) {
  134.             $salesChannelId $data['salesChannelId'];
  135.         }
  136.         if (empty($salesChannelId) && method_exists($context->getSource(), 'getSalesChannelId')) {
  137.             $salesChannelId $context->getSource()->getSalesChannelId();
  138.         }
  139.         if (!empty($salesChannelId)) {
  140.             $this->configService->setSalesChannelId($salesChannelId);
  141.         }
  142.         if (!$this->configService->getSubscriptionMailing()) {
  143.             return;
  144.         }
  145.         $event->stopPropagation();
  146.     }
  147.     /**
  148.      * @param string $templateId
  149.      * @param Context $context
  150.      * @return bool
  151.      */
  152.     private function isNewsletterMailTemplate(string $templateIdContext $context)
  153.     {
  154.         $criteria = new Criteria([$templateId]);
  155.         $criteria->addAssociation('mailTemplateType');
  156.         $mailTemplate $this->mailTemplateRepository->search($criteria$context)->first();
  157.         if (empty($mailTemplate)) {
  158.             return false;
  159.         }
  160.         return in_array($mailTemplate->getMailTemplateType()->getTechnicalName(), ['newsletterDoubleOptIn''newsletterRegister']);
  161.     }
  162. }