custom/plugins/NenoMarketingEssentials/src/Storefront/Subscriber/NewsletterConfirmedSubscriber.php line 57

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Neno\MarketingEssentials\Storefront\Subscriber;
  3. use Neno\MarketingEssentials\Service\PromotionCodeHelper;
  4. use Shopware\Core\Checkout\Promotion\PromotionEntity;
  5. use Shopware\Core\Content\Newsletter\Event\NewsletterConfirmEvent;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Neno\MarketingEssentials\Service\SendPromotionMail;
  12. class NewsletterConfirmedSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var EntityRepositoryInterface
  16.      */
  17.     private $promotionRepository;
  18.     /**
  19.      * @var SendPromotionMail
  20.      */
  21.     private $sendPromotionMail;
  22.     private PromotionCodeHelper $promotionCodeHelper;
  23.     public function __construct
  24.     (
  25.         EntityRepositoryInterface $promotionRepository,
  26.         PromotionCodeHelper $promotionCodeHelper,
  27.         SendPromotionMail $sendPromotionMail
  28.     ) {
  29.         $this->promotionRepository $promotionRepository;
  30.         $this->promotionCodeHelper $promotionCodeHelper;
  31.         $this->sendPromotionMail $sendPromotionMail;
  32.     }
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             NewsletterConfirmEvent::class => 'listenToConfirmation'
  37.         ];
  38.     }
  39.     private function fetchPromotionById(string $promotionIdContext $context): ?PromotionEntity {
  40.         if (!$promotionId) {
  41.             return null;
  42.         }
  43.         $promotionCriteria = new Criteria([$promotionId]);
  44.         $promotionCriteria->addAssociation('individualCodes');
  45.         return $this->promotionRepository->search($promotionCriteria$context)->first();
  46.     }
  47.     public function listenToConfirmation(NewsletterConfirmEvent $event): void {
  48.         $recipient $event->getNewsletterRecipient();
  49.         $recipientCustomFields $recipient->getCustomFields();
  50.         if ($recipientCustomFields === null) {
  51.             return;
  52.         }
  53.         $promotionId $recipientCustomFields['neno_nme_newsletter_promotion_id'];
  54.         if (!$promotionId) { return; }
  55.         $promotion $this->fetchPromotionById($promotionId$event->getContext());
  56.         if (!$promotion) {
  57.             return;
  58.             // throw new \Exception('No promotion found for this recipient');
  59.         }
  60.         $this->promotionCodeHelper
  61.             ->handleIndividualPromotionCodes($promotion$event->getContext());
  62.         $this->sendPromotionMail
  63.             ->sendPromotionMail(
  64.                 $promotion,
  65.                 $event->getSalesChannelId(),
  66.                 $event->getContext(),
  67.                 $recipient);
  68.     }
  69. }