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

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