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

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