custom/plugins/NenoMarketingEssentials/src/Storefront/Subscriber/CustomerRegisterSubscriber.php line 132

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Neno\MarketingEssentials\Storefront\Subscriber;
  3. use Neno\MarketingEssentials\Core\Event\CustomerRegisterWithPromotionEvent;
  4. use Neno\MarketingEssentials\Service\PromotionCodeHelper;
  5. use Neno\MarketingEssentials\Service\SendCustomerRegistrationPromotionMail;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  8. use Shopware\Core\Checkout\Promotion\PromotionEntity;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class CustomerRegisterSubscriber implements EventSubscriberInterface
  14. {
  15.     private EntityRepositoryInterface $promotionRepository;
  16.     private EntityRepositoryInterface $customerRepository;
  17.     private PromotionCodeHelper $promotionCodeHelper;
  18.     private SendCustomerRegistrationPromotionMail $sendCustomerRegistrationPromotionMail;
  19.     public function __construct
  20.     (
  21.         EntityRepositoryInterface $promotionRepository,
  22.         EntityRepositoryInterface $customerRepository,
  23.         PromotionCodeHelper $promotionCodeHelper,
  24.         SendCustomerRegistrationPromotionMail $sendCustomerRegistrationPromotionMail
  25.     ) {
  26.         $this->promotionRepository $promotionRepository;
  27.         $this->customerRepository $customerRepository;
  28.         $this->promotionCodeHelper $promotionCodeHelper;
  29.         $this->sendCustomerRegistrationPromotionMail $sendCustomerRegistrationPromotionMail;
  30.     }
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             CustomerRegisterWithPromotionEvent::class => 'listenToCustomerRegistration',
  35.             CustomerRegisterEvent::class => 'handleDoubleOptInRegistration'
  36.         ];
  37.     }
  38.     private function setCustomerDoubleOptInFlag(CustomerEntity $customerContext $context) {
  39.         $oldCustomFields $customer->getCustomFields();
  40.         if (!$oldCustomFields) {
  41.             $oldCustomFields = [];
  42.         }
  43.         $newCustomFields array_merge(
  44.             $oldCustomFields,
  45.             ['neno_nme_register_promotion_doi' => true]
  46.         );
  47.         $this->customerRepository->update([
  48.             [
  49.                 'id' => $customer->getId(),
  50.                 'customFields' => $newCustomFields,
  51.             ]
  52.         ], $context);
  53.     }
  54.     private function customerHasDoubleOptInPromotion(CustomerEntity $customer): bool {
  55.         $customFields $customer->getCustomFields();
  56.         if (!$customer->getDoubleOptInRegistration() || $customFields === null) {
  57.             return false;
  58.         }
  59.         return (
  60.             array_key_exists('neno_nme_register_promotion_id'$customFields) &&
  61.             $customFields['neno_nme_register_promotion_id'] &&
  62.             array_key_exists('neno_nme_register_promotion_doi'$customFields)
  63.         );
  64.     }
  65.     private function getPromotionIdFromCustomerCustomFields(CustomerEntity $customer): ?string {
  66.         $customerCustomFields $customer->getCustomFields();
  67.         if ($customerCustomFields === null) {
  68.             return null;
  69.         }
  70.         return $customerCustomFields['neno_nme_register_promotion_id'];
  71.     }
  72.     private function fetchPromotionById(string $promotionIdContext $context): ?PromotionEntity {
  73.         if (!$promotionId) {
  74.             return null;
  75.         }
  76.         $promotionCriteria = new Criteria([$promotionId]);
  77.         $promotionCriteria->addAssociation('individualCodes');
  78.         return $this->promotionRepository->search($promotionCriteria$context)->first();
  79.     }
  80.     public function listenToCustomerRegistration(CustomerRegisterWithPromotionEvent $event):void {
  81.         $customer $event->getCustomer();
  82.         if ($customer->getDoubleOptInRegistration()) {
  83.             $this->setCustomerDoubleOptInFlag($customer$event->getContext());
  84.             // Wait until we receive the doi confirmation
  85.             return;
  86.         }
  87.         $promotionId $event->getPromotionId();
  88.         if (!$promotionId) {
  89.             $promotionId $this->getPromotionIdFromCustomerCustomFields($customer);
  90.         }
  91.         if (!$promotionId) { return; }
  92.         $promotion $this->fetchPromotionById($promotionId$event->getContext());
  93.         if (!$promotion) { return; }
  94.         $this->promotionCodeHelper
  95.             ->handleIndividualPromotionCodes($promotion$event->getContext());
  96.         $this->sendCustomerRegistrationPromotionMail
  97.             ->sendRegistrationPromotionMail(
  98.                 $promotion,
  99.                 $event->getSalesChannelId(),
  100.                 $event->getContext(),
  101.                 $customer);
  102.     }
  103.     public function handleDoubleOptInRegistration(CustomerRegisterEvent $event) {
  104.         $customer $event->getCustomer();
  105.         if (!$this->customerHasDoubleOptInPromotion($customer)) {
  106.             return;
  107.         }
  108.         $promotionId $this->getPromotionIdFromCustomerCustomFields($customer);
  109.         if (!$promotionId) { return; }
  110.         $promotion $this->fetchPromotionById($promotionId$event->getContext());
  111.         if (!$promotion) { return; }
  112.         $this->promotionCodeHelper
  113.             ->handleIndividualPromotionCodes($promotion$event->getContext());
  114.         $this->sendCustomerRegistrationPromotionMail
  115.             ->sendRegistrationPromotionMail(
  116.                 $promotion,
  117.                 $event->getSalesChannelId(),
  118.                 $event->getContext(),
  119.                 $customer);
  120.     }
  121. }