platform/src/Core/Checkout/Customer/Subscriber/CustomerFlowEventsSubscriber.php line 43

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.     public static function getSubscribedEvents()
  30.     {
  31.         return [
  32.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWritten',
  33.         ];
  34.     }
  35.     public function onCustomerWritten(EntityWrittenEvent $event): void
  36.     {
  37.         if ($event->getContext()->getSource() instanceof SalesChannelApiSource) {
  38.             return;
  39.         }
  40.         $payloads $event->getPayloads();
  41.         foreach ($payloads as $payload) {
  42.             if (!empty($payload['defaultPaymentMethodId']) && empty($payload['createdAt'])) {
  43.                 $this->dispatchCustomerChangePaymentMethodEvent($payload['id'], $event);
  44.                 continue;
  45.             }
  46.             if (!empty($payload['createdAt'])) {
  47.                 $this->dispatchCustomerRegisterEvent($payload['id'], $event);
  48.             }
  49.         }
  50.     }
  51.     private function getSalesChannelId(string $customerId): string
  52.     {
  53.         $salesChannelId $this->connection->createQueryBuilder()
  54.             ->select('sales_channel_id')
  55.             ->from('customer')
  56.             ->where('id = :id')
  57.             ->setParameter(':id'Uuid::fromHexToBytes($customerId))
  58.             ->execute()
  59.             ->fetchColumn();
  60.         return $salesChannelId Uuid::fromBytesToHex($salesChannelId) : '';
  61.     }
  62.     private function dispatchCustomerRegisterEvent(string $customerIdEntityWrittenEvent $event): void
  63.     {
  64.         $context $event->getContext();
  65.         $salesChannelContext $this->salesChannelContextService->get(
  66.             new SalesChannelContextServiceParameters(
  67.                 $this->getSalesChannelId($customerId),
  68.                 Uuid::randomHex(),
  69.                 $context->getLanguageId(),
  70.                 null,
  71.                 null,
  72.                 $context,
  73.                 $customerId
  74.             )
  75.         );
  76.         if (!$customer $salesChannelContext->getCustomer()) {
  77.             return;
  78.         }
  79.         $customerCreated = new CustomerRegisterEvent(
  80.             $salesChannelContext,
  81.             $customer
  82.         );
  83.         $this->dispatcher->dispatch($customerCreated);
  84.     }
  85.     private function dispatchCustomerChangePaymentMethodEvent(string $customerIdEntityWrittenEvent $event): void
  86.     {
  87.         $context $event->getContext();
  88.         $salesChannelContext $this->salesChannelContextService->get(
  89.             new SalesChannelContextServiceParameters(
  90.                 $this->getSalesChannelId($customerId),
  91.                 Uuid::randomHex(),
  92.                 $context->getLanguageId(),
  93.                 null,
  94.                 null,
  95.                 $context,
  96.                 $customerId
  97.             )
  98.         );
  99.         if (!$customer $salesChannelContext->getCustomer()) {
  100.             return;
  101.         }
  102.         $customerChangePaymentMethodEvent = new CustomerChangedPaymentMethodEvent(
  103.             $salesChannelContext,
  104.             $customer,
  105.             new RequestDataBag()
  106.         );
  107.         $this->dispatcher->dispatch($customerChangePaymentMethodEvent);
  108.     }
  109. }