<?php declare(strict_types=1);
namespace Dvdw\PlatformChoice\Checkout\Customer\Subscriber;
use Dvdw\PlatformChoice\System\SalesChannel\Struct\PlatformChoiceExtension;
use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
use Shopware\Core\Content\Newsletter\SalesChannel\NewsletterSubscribeRoute;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomerRegisterSubscriber implements EventSubscriberInterface
{
private EntityRepository $recipientRepo;
private SalesChannelContextPersister $contextPersister;
public function __construct(
EntityRepository $recipientRepo,
SalesChannelContextPersister $contextPersister
)
{
$this->recipientRepo = $recipientRepo;
$this->contextPersister = $contextPersister;
}
public static function getSubscribedEvents(): iterable
{
return [
CustomerRegisterEvent::class =>
[
['extendCustomer', 100],
['subscribeNewsletter', 0],
['updateContext', 0]
]
];
}
public function extendCustomer(CustomerRegisterEvent $event): void
{
$event->getCustomer()->setGroup($event->getSalesChannelContext()->getCurrentCustomerGroup());
}
public function subscribeNewsletter(CustomerRegisterEvent $event): void
{
$customer = $event->getCustomer();
if ($customer->getGroup()->getDisplayGross()) {
return;
}
$salesChannelContext = $event->getSalesChannelContext();
$context = $salesChannelContext->getContext();
$email = $customer->getEmail();
if ($this->alreadySubscribed($email, $context)) {
return;
}
$recipient = [
'email' => $email,
'title' => $customer->getTitle(),
'firstName' => $customer->getFirstName(),
'lastName' => $customer->getLastName(),
'salutationId' => $customer->getSalutationId(),
'languageId' => $customer->getLanguageId(),
'salesChannelId' => $salesChannelContext->getSalesChannelId(),
'status' => NewsletterSubscribeRoute::STATUS_DIRECT,
'hash' => Uuid::randomHex()
];
$address = $customer->getDefaultBillingAddress();
if ($address !== null) {
$recipient['zipCode'] = $address->getZipcode();
$recipient['city'] = $address->getCity();
$recipient['street'] = $address->getStreet();
}
$this->recipientRepo->create([$recipient], $context);
}
public function updateContext(CustomerRegisterEvent $event): void
{
$choice = PlatformChoiceExtension::B2B;
if ($event->getCustomer()->getGroup()->getDisplayGross()) {
$choice = PlatformChoiceExtension::B2C;
}
$context = $event->getSalesChannelContext();
$this->contextPersister->save($context->getToken(), [PlatformChoiceExtension::KEY => $choice], '');
}
private function alreadySubscribed(string $email, Context $context): bool
{
$criteria = (new Criteria())->addFilter(new EqualsFilter('email', $email));
return $this->recipientRepo->searchIds($criteria, $context)->firstId() !== null;
}
}