<?php
namespace Selfdelve\Cms\Subscriber;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
use Shopware\Core\Content\Mail\Service\AbstractMailService;
use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Validation\DataBag\DataBag;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class CustomerRegisterSubscriber implements EventSubscriberInterface
{
private LoggerInterface $logger;
private EntityRepositoryInterface $customerGroupRepository;
private EntityRepositoryInterface $mailTemplateRepository;
private AbstractMailService $mailService;
private SystemConfigService $systemConfigService;
/**
* CustomerRegisterSubscriber constructor.
* @param LoggerInterface $logger
* @param EntityRepositoryInterface $customerGroupRepository
* @param EntityRepositoryInterface $mailTemplateRepository
* @param AbstractMailService $mailService
* @param SystemConfigService $systemConfigService
*/
public function __construct(
LoggerInterface $logger,
EntityRepositoryInterface $customerGroupRepository,
EntityRepositoryInterface $mailTemplateRepository,
AbstractMailService $mailService,
SystemConfigService $systemConfigService
) {
$this->logger = $logger;
$this->customerGroupRepository = $customerGroupRepository;
$this->mailTemplateRepository = $mailTemplateRepository;
$this->mailService = $mailService;
$this->systemConfigService = $systemConfigService;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
CustomerRegisterEvent::class => 'onCustomerRegister'
];
}
/**
* Email the admin, if a customer requested a closed customer group (e.g., reseller).
* This event is triggered after the initial opt-in of the customer.
*
* @param CustomerRegisterEvent $event
* @return bool
*/
public function onCustomerRegister(CustomerRegisterEvent $event): bool
{
/** @var CustomerEntity $customer */
$customer = $event->getCustomer();
if ((string) $customer->getRequestedGroupId() === '') {
return false;
}
/** @var CustomerGroupEntity $customerGroup */
$customerGroup = $this->customerGroupRepository->search(
new Criteria([$customer->getRequestedGroupId()]),
$event->getContext()
)->first();
if (!$customerGroup instanceof CustomerGroupEntity) {
return false;
}
if ($customerGroup->getRegistrationOnlyCompanyRegistration() === false) {
return false;
}
$mailTemplateId = $this->systemConfigService->get('SelfdelveCms.config.registrationTemplateId');
/** @var MailTemplateEntity $mailTemplate */
$mailTemplate = $this->mailTemplateRepository->search(
new Criteria([$mailTemplateId]),
$event->getContext()
)->first();
if (!$mailTemplate) {
$this->logger->error('Could not find email template for id ' . $mailTemplateId);
return false;
}
$data = new DataBag();
$data->set('salesChannelId', $event->getSalesChannelId());
$data->set('templateId', $mailTemplate->getId());
$data->set('recipients', [
$this->systemConfigService->get('SelfdelveCms.config.adminEmail') => $this->systemConfigService->get('SelfdelveCms.config.adminName')
]);
$data->set('senderName', $mailTemplate->getTranslation('senderName'));
$data->set('subject', $mailTemplate->getTranslation('subject'));
$data->set('contentHtml', $mailTemplate->getTranslation('contentHtml'));
$data->set('contentPlain', $mailTemplate->getTranslation('contentPlain'));
if (!$this->mailService->send(
$data->all(),
$event->getContext(),
['customer' => $customer]
)) {
$this->logger->error('Customer registration admin email not sent.');
return false;
}
return true;
}
}