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

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