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

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