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

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