vendor/shopware/core/Content/Flow/Dispatching/Action/AddOrderAffiliateAndCampaignCodeAction.php line 66

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\Content\Flow\Dispatching\DelayableAction;
  5. use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\Event\FlowEvent;
  9. use Shopware\Core\Framework\Event\OrderAware;
  10. use Shopware\Core\Framework\Feature;
  11. use Shopware\Core\Framework\Uuid\Uuid;
  12. /**
  13.  * @deprecated tag:v6.5.0 - reason:remove-subscriber - FlowActions won't be executed over the event system anymore,
  14.  * therefore the actions won't implement the EventSubscriberInterface anymore.
  15.  */
  16. class AddOrderAffiliateAndCampaignCodeAction extends FlowAction implements DelayableAction
  17. {
  18.     private Connection $connection;
  19.     private EntityRepositoryInterface $orderRepository;
  20.     /**
  21.      * @internal
  22.      */
  23.     public function __construct(
  24.         Connection $connection,
  25.         EntityRepositoryInterface $orderRepository
  26.     ) {
  27.         $this->connection $connection;
  28.         $this->orderRepository $orderRepository;
  29.     }
  30.     public static function getName(): string
  31.     {
  32.         return 'action.add.order.affiliate.and.campaign.code';
  33.     }
  34.     /**
  35.      * @deprecated tag:v6.5.0 - reason:remove-subscriber - Will be removed
  36.      */
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         if (Feature::isActive('v6.5.0.0')) {
  40.             return [];
  41.         }
  42.         return [
  43.             self::getName() => 'handle',
  44.         ];
  45.     }
  46.     /**
  47.      * @return array<int, string>
  48.      */
  49.     public function requirements(): array
  50.     {
  51.         return [OrderAware::class];
  52.     }
  53.     /**
  54.      * @deprecated tag:v6.5.0 Will be removed, implement handleFlow instead
  55.      */
  56.     public function handle(FlowEvent $event): void
  57.     {
  58.         Feature::triggerDeprecationOrThrow(
  59.             'v6.5.0.0',
  60.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0')
  61.         );
  62.         $baseEvent $event->getEvent();
  63.         if (!$baseEvent instanceof OrderAware) {
  64.             return;
  65.         }
  66.         $this->update($baseEvent->getContext(), $event->getConfig(), $baseEvent->getOrderId());
  67.     }
  68.     public function handleFlow(StorableFlow $flow): void
  69.     {
  70.         if (!$flow->hasStore(OrderAware::ORDER_ID)) {
  71.             return;
  72.         }
  73.         $this->update($flow->getContext(), $flow->getConfig(), $flow->getStore(OrderAware::ORDER_ID));
  74.     }
  75.     /**
  76.      * @return array<mixed>
  77.      */
  78.     private function getAffiliateAndCampaignCodeFromOrderId(string $orderId): array
  79.     {
  80.         $data $this->connection->fetchAssociative(
  81.             'SELECT affiliate_code, campaign_code FROM `order` WHERE id = :id',
  82.             [
  83.                 'id' => Uuid::fromHexToBytes($orderId),
  84.             ]
  85.         );
  86.         if (!$data) {
  87.             return [];
  88.         }
  89.         return $data;
  90.     }
  91.     /**
  92.      * @param array<string, mixed> $config
  93.      */
  94.     private function update(Context $context, array $configstring $orderId): void
  95.     {
  96.         if (!\array_key_exists('affiliateCode'$config) || !\array_key_exists('campaignCode'$config)) {
  97.             return;
  98.         }
  99.         $orderData $this->getAffiliateAndCampaignCodeFromOrderId($orderId);
  100.         if (empty($orderData)) {
  101.             return;
  102.         }
  103.         $affiliateCode $orderData['affiliate_code'];
  104.         if ($affiliateCode === null || $config['affiliateCode']['upsert']) {
  105.             $affiliateCode $config['affiliateCode']['value'];
  106.         }
  107.         $campaignCode $orderData['campaign_code'];
  108.         if ($campaignCode === null || $config['campaignCode']['upsert']) {
  109.             $campaignCode $config['campaignCode']['value'];
  110.         }
  111.         $data = [];
  112.         if ($affiliateCode !== $orderData['affiliate_code']) {
  113.             $data['affiliateCode'] = $affiliateCode;
  114.         }
  115.         if ($campaignCode !== $orderData['campaign_code']) {
  116.             $data['campaignCode'] = $campaignCode;
  117.         }
  118.         if (empty($data)) {
  119.             return;
  120.         }
  121.         $data['id'] = $orderId;
  122.         $this->orderRepository->update([$data], $context);
  123.     }
  124. }