vendor/shopware/core/Content/Flow/Dispatching/Action/AddCustomerAffiliateAndCampaignCodeAction.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Flow\Dispatching\Action;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\Event\CustomerAware;
  6. use Shopware\Core\Framework\Event\DelayAware;
  7. use Shopware\Core\Framework\Event\FlowEvent;
  8. use Shopware\Core\Framework\Uuid\Uuid;
  9. class AddCustomerAffiliateAndCampaignCodeAction extends FlowAction
  10. {
  11.     private Connection $connection;
  12.     private EntityRepositoryInterface $customerRepository;
  13.     /**
  14.      * @internal
  15.      */
  16.     public function __construct(
  17.         Connection $connection,
  18.         EntityRepositoryInterface $customerRepository
  19.     ) {
  20.         $this->connection $connection;
  21.         $this->customerRepository $customerRepository;
  22.     }
  23.     public static function getName(): string
  24.     {
  25.         return 'action.add.customer.affiliate.and.campaign.code';
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             self::getName() => 'handle',
  31.         ];
  32.     }
  33.     public function requirements(): array
  34.     {
  35.         return [CustomerAware::class, DelayAware::class];
  36.     }
  37.     public function handle(FlowEvent $event): void
  38.     {
  39.         $config $event->getConfig();
  40.         if (!\array_key_exists('affiliateCode'$config) || !\array_key_exists('campaignCode'$config)) {
  41.             return;
  42.         }
  43.         $baseEvent $event->getEvent();
  44.         if (!$baseEvent instanceof CustomerAware) {
  45.             return;
  46.         }
  47.         $customerId $baseEvent->getCustomerId();
  48.         $customerData $this->getAffiliateAndCampaignCodeFromCustomerId($customerId);
  49.         if (empty($customerData)) {
  50.             return;
  51.         }
  52.         $affiliateCode $customerData['affiliate_code'];
  53.         if ($affiliateCode === null || $config['affiliateCode']['upsert']) {
  54.             $affiliateCode $config['affiliateCode']['value'];
  55.         }
  56.         $campaignCode $customerData['campaign_code'];
  57.         if ($campaignCode === null || $config['campaignCode']['upsert']) {
  58.             $campaignCode $config['campaignCode']['value'];
  59.         }
  60.         $data = [];
  61.         if ($affiliateCode !== $customerData['affiliate_code']) {
  62.             $data['affiliateCode'] = $affiliateCode;
  63.         }
  64.         if ($campaignCode !== $customerData['campaign_code']) {
  65.             $data['campaignCode'] = $campaignCode;
  66.         }
  67.         if (empty($data)) {
  68.             return;
  69.         }
  70.         $data['id'] = $customerId;
  71.         $this->customerRepository->update([$data], $baseEvent->getContext());
  72.     }
  73.     private function getAffiliateAndCampaignCodeFromCustomerId(string $customerId): array
  74.     {
  75.         $query $this->connection->createQueryBuilder();
  76.         $query->select(['affiliate_code''campaign_code']);
  77.         $query->from('customer');
  78.         $query->where('id = :id');
  79.         $query->setParameter('id'Uuid::fromHexToBytes($customerId));
  80.         if (!$data $query->execute()->fetchAssociative()) {
  81.             return [];
  82.         }
  83.         return $data;
  84.     }
  85. }