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

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.     public function __construct(
  17.         EventDispatcherInterface $dispatcher,
  18.         SalesChannelContextRestorer $restorer
  19.     ) {
  20.         $this->dispatcher $dispatcher;
  21.         $this->restorer $restorer;
  22.     }
  23.     /**
  24.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  25.      */
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWritten',
  30.         ];
  31.     }
  32.     public function onCustomerWritten(EntityWrittenEvent $event): void
  33.     {
  34.         if ($event->getContext()->getSource() instanceof SalesChannelApiSource) {
  35.             return;
  36.         }
  37.         $payloads $event->getPayloads();
  38.         foreach ($payloads as $payload) {
  39.             if (!empty($payload['defaultPaymentMethodId']) && empty($payload['createdAt'])) {
  40.                 $this->dispatchCustomerChangePaymentMethodEvent($payload['id'], $event);
  41.                 continue;
  42.             }
  43.             if (!empty($payload['createdAt'])) {
  44.                 $this->dispatchCustomerRegisterEvent($payload['id'], $event);
  45.             }
  46.         }
  47.     }
  48.     private function dispatchCustomerRegisterEvent(string $customerIdEntityWrittenEvent $event): void
  49.     {
  50.         $context $event->getContext();
  51.         $salesChannelContext $this->restorer->restoreByCustomer($customerId$context);
  52.         if (!$customer $salesChannelContext->getCustomer()) {
  53.             return;
  54.         }
  55.         $customerCreated = new CustomerRegisterEvent(
  56.             $salesChannelContext,
  57.             $customer
  58.         );
  59.         $this->dispatcher->dispatch($customerCreated);
  60.     }
  61.     private function dispatchCustomerChangePaymentMethodEvent(string $customerIdEntityWrittenEvent $event): void
  62.     {
  63.         $context $event->getContext();
  64.         $salesChannelContext $this->restorer->restoreByCustomer($customerId$context);
  65.         if (!$customer $salesChannelContext->getCustomer()) {
  66.             return;
  67.         }
  68.         $customerChangePaymentMethodEvent = new CustomerChangedPaymentMethodEvent(
  69.             $salesChannelContext,
  70.             $customer,
  71.             new RequestDataBag()
  72.         );
  73.         $this->dispatcher->dispatch($customerChangePaymentMethodEvent);
  74.     }
  75. }