vendor/shopware/core/Checkout/Customer/Subscriber/CustomerFlowEventsSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Customer\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEvents;
  4. use Shopware\Core\Checkout\Customer\Event\CustomerChangedPaymentMethodEvent;
  5. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  6. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  9. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextRestorer;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  12. class CustomerFlowEventsSubscriber implements EventSubscriberInterface
  13. {
  14.     private EventDispatcherInterface $dispatcher;
  15.     private SalesChannelContextRestorer $restorer;
  16.     /**
  17.      * @internal
  18.      */
  19.     public function __construct(
  20.         EventDispatcherInterface $dispatcher,
  21.         SalesChannelContextRestorer $restorer
  22.     ) {
  23.         $this->dispatcher $dispatcher;
  24.         $this->restorer $restorer;
  25.     }
  26.     /**
  27.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  28.      */
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return [
  32.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWritten',
  33.         ];
  34.     }
  35.     public function onCustomerWritten(EntityWrittenEvent $event): void
  36.     {
  37.         if ($event->getContext()->getSource() instanceof SalesChannelApiSource) {
  38.             return;
  39.         }
  40.         $payloads $event->getPayloads();
  41.         foreach ($payloads as $payload) {
  42.             if (!empty($payload['defaultPaymentMethodId']) && empty($payload['createdAt'])) {
  43.                 $this->dispatchCustomerChangePaymentMethodEvent($payload['id'], $event);
  44.                 continue;
  45.             }
  46.             if (!empty($payload['createdAt'])) {
  47.                 $this->dispatchCustomerRegisterEvent($payload['id'], $event);
  48.             }
  49.         }
  50.     }
  51.     private function dispatchCustomerRegisterEvent(string $customerIdEntityWrittenEvent $event): void
  52.     {
  53.         $context $event->getContext();
  54.         $salesChannelContext $this->restorer->restoreByCustomer($customerId$context);
  55.         if (!$customer $salesChannelContext->getCustomer()) {
  56.             return;
  57.         }
  58.         $customerCreated = new CustomerRegisterEvent(
  59.             $salesChannelContext,
  60.             $customer
  61.         );
  62.         $this->dispatcher->dispatch($customerCreated);
  63.     }
  64.     private function dispatchCustomerChangePaymentMethodEvent(string $customerIdEntityWrittenEvent $event): void
  65.     {
  66.         $context $event->getContext();
  67.         $salesChannelContext $this->restorer->restoreByCustomer($customerId$context);
  68.         if (!$customer $salesChannelContext->getCustomer()) {
  69.             return;
  70.         }
  71.         $customerChangePaymentMethodEvent = new CustomerChangedPaymentMethodEvent(
  72.             $salesChannelContext,
  73.             $customer,
  74.             new RequestDataBag()
  75.         );
  76.         $this->dispatcher->dispatch($customerChangePaymentMethodEvent);
  77.     }
  78. }