vendor/shopware/core/Content/Flow/Dispatching/Action/AddOrderAffiliateAndCampaignCodeAction.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\DelayAware;
  6. use Shopware\Core\Framework\Event\FlowEvent;
  7. use Shopware\Core\Framework\Event\OrderAware;
  8. use Shopware\Core\Framework\Uuid\Uuid;
  9. class AddOrderAffiliateAndCampaignCodeAction extends FlowAction
  10. {
  11.     private Connection $connection;
  12.     private EntityRepositoryInterface $orderRepository;
  13.     /**
  14.      * @internal
  15.      */
  16.     public function __construct(
  17.         Connection $connection,
  18.         EntityRepositoryInterface $orderRepository
  19.     ) {
  20.         $this->connection $connection;
  21.         $this->orderRepository $orderRepository;
  22.     }
  23.     public static function getName(): string
  24.     {
  25.         return 'action.add.order.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 [OrderAware::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 OrderAware) {
  45.             return;
  46.         }
  47.         $orderId $baseEvent->getOrderId();
  48.         $orderData $this->getAffiliateAndCampaignCodeFromOrderId($orderId);
  49.         if (empty($orderData)) {
  50.             return;
  51.         }
  52.         $affiliateCode $orderData['affiliate_code'];
  53.         if ($affiliateCode === null || $config['affiliateCode']['upsert']) {
  54.             $affiliateCode $config['affiliateCode']['value'];
  55.         }
  56.         $campaignCode $orderData['campaign_code'];
  57.         if ($campaignCode === null || $config['campaignCode']['upsert']) {
  58.             $campaignCode $config['campaignCode']['value'];
  59.         }
  60.         $data = [];
  61.         if ($affiliateCode !== $orderData['affiliate_code']) {
  62.             $data['affiliateCode'] = $affiliateCode;
  63.         }
  64.         if ($campaignCode !== $orderData['campaign_code']) {
  65.             $data['campaignCode'] = $campaignCode;
  66.         }
  67.         if (empty($data)) {
  68.             return;
  69.         }
  70.         $data['id'] = $orderId;
  71.         $this->orderRepository->update([$data], $baseEvent->getContext());
  72.     }
  73.     private function getAffiliateAndCampaignCodeFromOrderId(string $orderId): array
  74.     {
  75.         $query $this->connection->createQueryBuilder();
  76.         $query->select(['affiliate_code''campaign_code']);
  77.         $query->from('`order`');
  78.         $query->where('id = :id');
  79.         $query->setParameter('id'Uuid::fromHexToBytes($orderId));
  80.         if (!$data $query->execute()->fetchAssociative()) {
  81.             return [];
  82.         }
  83.         return $data;
  84.     }
  85. }