vendor/dvdw/platform-choice/src/Checkout/Customer/Subscriber/CustomerRegisterSubscriber.php line 87

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dvdw\PlatformChoice\Checkout\Customer\Subscriber;
  3. use Dvdw\PlatformChoice\System\SalesChannel\Struct\PlatformChoiceExtension;
  4. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  5. use Shopware\Core\Content\Newsletter\SalesChannel\NewsletterSubscribeRoute;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\Uuid\Uuid;
  11. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class CustomerRegisterSubscriber implements EventSubscriberInterface
  14. {
  15.     private EntityRepository $recipientRepo;
  16.     private SalesChannelContextPersister $contextPersister;
  17.     public function __construct(
  18.         EntityRepository $recipientRepo,
  19.         SalesChannelContextPersister $contextPersister
  20.     )
  21.     {
  22.         $this->recipientRepo $recipientRepo;
  23.         $this->contextPersister $contextPersister;
  24.     }
  25.     public static function getSubscribedEvents(): iterable
  26.     {
  27.         return [
  28.             CustomerRegisterEvent::class =>
  29.                 [
  30.                     ['extendCustomer'100],
  31.                     ['subscribeNewsletter'0],
  32.                     ['updateContext'0]
  33.                 ]
  34.         ];
  35.     }
  36.     public function extendCustomer(CustomerRegisterEvent $event): void
  37.     {
  38.         $event->getCustomer()->setGroup($event->getSalesChannelContext()->getCurrentCustomerGroup());
  39.     }
  40.     public function subscribeNewsletter(CustomerRegisterEvent $event): void
  41.     {
  42.         $customer $event->getCustomer();
  43.         if ($customer->getGroup()->getDisplayGross()) {
  44.             return;
  45.         }
  46.         $salesChannelContext $event->getSalesChannelContext();
  47.         $context $salesChannelContext->getContext();
  48.         $email $customer->getEmail();
  49.         if ($this->alreadySubscribed($email$context)) {
  50.             return;
  51.         }
  52.         $recipient = [
  53.             'email' => $email,
  54.             'title' => $customer->getTitle(),
  55.             'firstName' => $customer->getFirstName(),
  56.             'lastName' => $customer->getLastName(),
  57.             'salutationId' => $customer->getSalutationId(),
  58.             'languageId' => $customer->getLanguageId(),
  59.             'salesChannelId' => $salesChannelContext->getSalesChannelId(),
  60.             'status' => NewsletterSubscribeRoute::STATUS_DIRECT,
  61.             'hash' => Uuid::randomHex()
  62.         ];
  63.         $address $customer->getDefaultBillingAddress();
  64.         if ($address !== null) {
  65.             $recipient['zipCode'] = $address->getZipcode();
  66.             $recipient['city'] = $address->getCity();
  67.             $recipient['street'] = $address->getStreet();
  68.         }
  69.         $this->recipientRepo->create([$recipient], $context);
  70.     }
  71.     public function updateContext(CustomerRegisterEvent $event): void
  72.     {
  73.         $choice PlatformChoiceExtension::B2B;
  74.         if ($event->getCustomer()->getGroup()->getDisplayGross()) {
  75.             $choice PlatformChoiceExtension::B2C;
  76.         }
  77.         $context $event->getSalesChannelContext();
  78.         $this->contextPersister->save($context->getToken(), [PlatformChoiceExtension::KEY => $choice], '');
  79.     }
  80.     private function alreadySubscribed(string $emailContext $context): bool
  81.     {
  82.         $criteria = (new Criteria())->addFilter(new EqualsFilter('email'$email));
  83.         return $this->recipientRepo->searchIds($criteria$context)->firstId() !== null;
  84.     }
  85. }