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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Customer\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Customer\CustomerEvents;
  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\Uuid\Uuid;
  10. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  11. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceInterface;
  12. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceParameters;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  15. class CustomerFlowEventsSubscriber implements EventSubscriberInterface
  16. {
  17.     private EventDispatcherInterface $dispatcher;
  18.     private Connection $connection;
  19.     private SalesChannelContextServiceInterface $salesChannelContextService;
  20.     public function __construct(
  21.         Connection $connection,
  22.         EventDispatcherInterface $dispatcher,
  23.         SalesChannelContextServiceInterface $salesChannelContextService
  24.     ) {
  25.         $this->connection $connection;
  26.         $this->dispatcher $dispatcher;
  27.         $this->salesChannelContextService $salesChannelContextService;
  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 getSalesChannelId(string $customerId): string
  55.     {
  56.         $salesChannelId $this->connection->createQueryBuilder()
  57.             ->select('sales_channel_id')
  58.             ->from('customer')
  59.             ->where('id = :id')
  60.             ->setParameter(':id'Uuid::fromHexToBytes($customerId))
  61.             ->execute()
  62.             ->fetchColumn();
  63.         return $salesChannelId Uuid::fromBytesToHex($salesChannelId) : '';
  64.     }
  65.     private function dispatchCustomerRegisterEvent(string $customerIdEntityWrittenEvent $event): void
  66.     {
  67.         $context $event->getContext();
  68.         $salesChannelContext $this->salesChannelContextService->get(
  69.             new SalesChannelContextServiceParameters(
  70.                 $this->getSalesChannelId($customerId),
  71.                 Uuid::randomHex(),
  72.                 $context->getLanguageId(),
  73.                 null,
  74.                 null,
  75.                 $context,
  76.                 $customerId
  77.             )
  78.         );
  79.         if (!$customer $salesChannelContext->getCustomer()) {
  80.             return;
  81.         }
  82.         $customerCreated = new CustomerRegisterEvent(
  83.             $salesChannelContext,
  84.             $customer
  85.         );
  86.         $this->dispatcher->dispatch($customerCreated);
  87.     }
  88.     private function dispatchCustomerChangePaymentMethodEvent(string $customerIdEntityWrittenEvent $event): void
  89.     {
  90.         $context $event->getContext();
  91.         $salesChannelContext $this->salesChannelContextService->get(
  92.             new SalesChannelContextServiceParameters(
  93.                 $this->getSalesChannelId($customerId),
  94.                 Uuid::randomHex(),
  95.                 $context->getLanguageId(),
  96.                 null,
  97.                 null,
  98.                 $context,
  99.                 $customerId
  100.             )
  101.         );
  102.         if (!$customer $salesChannelContext->getCustomer()) {
  103.             return;
  104.         }
  105.         $customerChangePaymentMethodEvent = new CustomerChangedPaymentMethodEvent(
  106.             $salesChannelContext,
  107.             $customer,
  108.             new RequestDataBag()
  109.         );
  110.         $this->dispatcher->dispatch($customerChangePaymentMethodEvent);
  111.     }
  112. }