custom/plugins/SelfdelveCms/src/Subscriber/CustomerRegisterSubscriber.php line 69

Open in your IDE?
  1. <?php
  2.     namespace Selfdelve\Cms\Subscriber;
  3.     use Psr\Log\LoggerInterface;
  4.     use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity;
  5.     use Shopware\Core\Checkout\Customer\CustomerEntity;
  6.     use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  7.     use Shopware\Core\Content\Mail\Service\AbstractMailService;
  8.     use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
  9.     use Shopware\Core\Framework\Context;
  10.     use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11.     use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12.     use Shopware\Core\Framework\Validation\DataBag\DataBag;
  13.     use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14.     use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15.     use Shopware\Core\System\SystemConfig\SystemConfigService;
  16.     class CustomerRegisterSubscriber implements EventSubscriberInterface
  17.     {
  18.         private LoggerInterface $logger;
  19.         private EntityRepositoryInterface $customerGroupRepository;
  20.         private EntityRepositoryInterface $mailTemplateRepository;
  21.         private AbstractMailService $mailService;
  22.         private SystemConfigService $systemConfigService;
  23.         /**
  24.          * CustomerRegisterSubscriber constructor.
  25.          * @param LoggerInterface $logger
  26.          * @param EntityRepositoryInterface $customerGroupRepository
  27.          * @param EntityRepositoryInterface $mailTemplateRepository
  28.          * @param AbstractMailService $mailService
  29.          * @param SystemConfigService $systemConfigService
  30.          */
  31.         public function __construct(
  32.             LoggerInterface $logger,
  33.             EntityRepositoryInterface $customerGroupRepository,
  34.             EntityRepositoryInterface $mailTemplateRepository,
  35.             AbstractMailService $mailService,
  36.             SystemConfigService $systemConfigService
  37.         ) {
  38.             $this->logger $logger;
  39.             $this->customerGroupRepository $customerGroupRepository;
  40.             $this->mailTemplateRepository $mailTemplateRepository;
  41.             $this->mailService $mailService;
  42.             $this->systemConfigService $systemConfigService;
  43.         }
  44.         /**
  45.          * @return array
  46.          */
  47.         public static function getSubscribedEvents(): array
  48.         {
  49.             return [
  50.                 CustomerRegisterEvent::class => 'onCustomerRegister'
  51.             ];
  52.         }
  53.         /**
  54.          * Email the admin, if a customer requested a closed customer group (e.g., reseller).
  55.          * This event is triggered after the initial opt-in of the customer.
  56.          *
  57.          * @param CustomerRegisterEvent $event
  58.          * @return bool
  59.          */
  60.         public function onCustomerRegister(CustomerRegisterEvent $event): bool
  61.         {
  62.             /** @var CustomerEntity $customer */
  63.             $customer $event->getCustomer();
  64.             if ((string) $customer->getRequestedGroupId() === '') {
  65.                 return false;
  66.             }
  67.             /** @var CustomerGroupEntity $customerGroup */
  68.             $customerGroup $this->customerGroupRepository->search(
  69.                 new Criteria([$customer->getRequestedGroupId()]),
  70.                 $event->getContext()
  71.             )->first();
  72.             if (!$customerGroup instanceof CustomerGroupEntity) {
  73.                 return false;
  74.             }
  75.             if ($customerGroup->getRegistrationOnlyCompanyRegistration() === false) {
  76.                 return false;
  77.             }
  78.             $mailTemplateId $this->systemConfigService->get('SelfdelveCms.config.registrationTemplateId');
  79.             /** @var MailTemplateEntity $mailTemplate */
  80.             $mailTemplate $this->mailTemplateRepository->search(
  81.                 new Criteria([$mailTemplateId]),
  82.                 $event->getContext()
  83.             )->first();
  84.             if (!$mailTemplate) {
  85.                 $this->logger->error('Could not find email template for id ' $mailTemplateId);
  86.                 return false;
  87.             }
  88.             $data = new DataBag();
  89.             $data->set('salesChannelId'$event->getSalesChannelId());
  90.             $data->set('templateId'$mailTemplate->getId());
  91.             $data->set('recipients', [
  92.                 $this->systemConfigService->get('SelfdelveCms.config.adminEmail') => $this->systemConfigService->get('SelfdelveCms.config.adminName')
  93.             ]);
  94.             $data->set('senderName'$mailTemplate->getTranslation('senderName'));
  95.             $data->set('subject'$mailTemplate->getTranslation('subject'));
  96.             $data->set('contentHtml'$mailTemplate->getTranslation('contentHtml'));
  97.             $data->set('contentPlain'$mailTemplate->getTranslation('contentPlain'));
  98.             if (!$this->mailService->send(
  99.                 $data->all(),
  100.                 $event->getContext(),
  101.                 ['customer' => $customer]
  102.             )) {
  103.                 $this->logger->error('Customer registration admin email not sent.');
  104.                 return false;
  105.             }
  106.             return true;
  107.         }
  108.     }