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

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