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

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