<?php declare(strict_types=1);
namespace Compra\EsiSW6\Core\Checkout\Customer\Subscriber;
use Shopware\Core\Checkout\Customer\Event\CustomerChangedPaymentMethodEvent;
use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
use Shopware\Core\Framework\Api\Context\AdminApiSource;
use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextRestorer;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class CustomerFlowEventsSubscriber extends \Shopware\Core\Checkout\Customer\Subscriber\CustomerFlowEventsSubscriber
{
private EventDispatcherInterface $dispatcher;
private SalesChannelContextRestorer $restorer;
public function __construct(
EventDispatcherInterface $dispatcher,
SalesChannelContextRestorer $restorer
) {
$this->dispatcher = $dispatcher;
$this->restorer = $restorer;
parent::__construct(
$dispatcher,
$restorer
);
}
public function onCustomerWritten(EntityWrittenEvent $event): void
{
if ($event->getContext()->getSource() instanceof SalesChannelApiSource) {
return;
}
$payloads = $event->getPayloads();
foreach ($payloads as $payload) {
// COMPRA change: check for CustomField "compra_is_esi_source" and source of event
$customFields = $payload['customFields'] ?? [];
$isEsiSource = array_key_exists('compra_is_esi_source', $customFields) ? $customFields['compra_is_esi_source'] : false;
$isAdminApiSource = ($event->getContext()->getSource() instanceof AdminApiSource);
/*
* Adapt the original logic and add additional condition:
* Only dispatch to CustomerChangePaymentMethodEvent if
* - "compra_is_esi_source" isn't set to true OR
* - source of event is not an AdminApiSource
*/
if (!empty($payload['defaultPaymentMethodId']) && empty($payload['createdAt'])
&& (!$isEsiSource || !$isAdminApiSource)
) {
$this->dispatchCustomerChangePaymentMethodEvent($payload['id'], $event);
continue;
}
/*
* Adapt the original logic and add additional condition:
* Only dispatch to CustomerRegisterEvent if
* - "compra_is_esi_source" isn't set to true OR
* - source of event is not an AdminApiSource
*/
if (!empty($payload['createdAt'])
&& (!$isEsiSource || !$isAdminApiSource)
) {
$this->dispatchCustomerRegisterEvent($payload['id'], $event);
}
}
}
private function dispatchCustomerRegisterEvent(string $customerId, EntityWrittenEvent $event): void
{
$context = $event->getContext();
$salesChannelContext = $this->restorer->restoreByCustomer($customerId, $context);
if (!$customer = $salesChannelContext->getCustomer()) {
return;
}
$customerCreated = new CustomerRegisterEvent(
$salesChannelContext,
$customer
);
$this->dispatcher->dispatch($customerCreated);
}
private function dispatchCustomerChangePaymentMethodEvent(string $customerId, EntityWrittenEvent $event): void
{
$context = $event->getContext();
$salesChannelContext = $this->restorer->restoreByCustomer($customerId, $context);
if (!$customer = $salesChannelContext->getCustomer()) {
return;
}
$customerChangePaymentMethodEvent = new CustomerChangedPaymentMethodEvent(
$salesChannelContext,
$customer,
new RequestDataBag()
);
$this->dispatcher->dispatch($customerChangePaymentMethodEvent);
}
}