<?php declare(strict_types=1);
namespace Shopware\Core\Content\Flow\Dispatching\Action;
use Doctrine\DBAL\Connection;
use Shopware\Core\Content\Flow\Dispatching\DelayableAction;
use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\Event\FlowEvent;
use Shopware\Core\Framework\Event\OrderAware;
use Shopware\Core\Framework\Feature;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Uuid\Uuid;
/**
* @deprecated tag:v6.5.0 - reason:remove-subscriber - FlowActions won't be executed over the event system anymore,
* therefore the actions won't implement the EventSubscriberInterface anymore.
*/
#[Package('business-ops')]
class AddOrderAffiliateAndCampaignCodeAction extends FlowAction implements DelayableAction
{
private Connection $connection;
private EntityRepositoryInterface $orderRepository;
/**
* @internal
*/
public function __construct(
Connection $connection,
EntityRepositoryInterface $orderRepository
) {
$this->connection = $connection;
$this->orderRepository = $orderRepository;
}
public static function getName(): string
{
return 'action.add.order.affiliate.and.campaign.code';
}
/**
* @deprecated tag:v6.5.0 - reason:remove-subscriber - Will be removed
*/
public static function getSubscribedEvents(): array
{
if (Feature::isActive('v6.5.0.0')) {
return [];
}
return [
self::getName() => 'handle',
];
}
/**
* @return array<int, string>
*/
public function requirements(): array
{
return [OrderAware::class];
}
/**
* @deprecated tag:v6.5.0 Will be removed, implement handleFlow instead
*/
public function handle(FlowEvent $event): void
{
Feature::triggerDeprecationOrThrow(
'v6.5.0.0',
Feature::deprecatedMethodMessage(__CLASS__, __METHOD__, 'v6.5.0.0')
);
$baseEvent = $event->getEvent();
if (!$baseEvent instanceof OrderAware) {
return;
}
$this->update($baseEvent->getContext(), $event->getConfig(), $baseEvent->getOrderId());
}
public function handleFlow(StorableFlow $flow): void
{
if (!$flow->hasStore(OrderAware::ORDER_ID)) {
return;
}
$this->update($flow->getContext(), $flow->getConfig(), $flow->getStore(OrderAware::ORDER_ID));
}
/**
* @return array<mixed>
*/
private function getAffiliateAndCampaignCodeFromOrderId(string $orderId): array
{
$data = $this->connection->fetchAssociative(
'SELECT affiliate_code, campaign_code FROM `order` WHERE id = :id',
[
'id' => Uuid::fromHexToBytes($orderId),
]
);
if (!$data) {
return [];
}
return $data;
}
/**
* @param array<string, mixed> $config
*/
private function update(Context $context, array $config, string $orderId): void
{
if (!\array_key_exists('affiliateCode', $config) || !\array_key_exists('campaignCode', $config)) {
return;
}
$orderData = $this->getAffiliateAndCampaignCodeFromOrderId($orderId);
if (empty($orderData)) {
return;
}
$affiliateCode = $orderData['affiliate_code'];
if ($affiliateCode === null || $config['affiliateCode']['upsert']) {
$affiliateCode = $config['affiliateCode']['value'];
}
$campaignCode = $orderData['campaign_code'];
if ($campaignCode === null || $config['campaignCode']['upsert']) {
$campaignCode = $config['campaignCode']['value'];
}
$data = [];
if ($affiliateCode !== $orderData['affiliate_code']) {
$data['affiliateCode'] = $affiliateCode;
}
if ($campaignCode !== $orderData['campaign_code']) {
$data['campaignCode'] = $campaignCode;
}
if (empty($data)) {
return;
}
$data['id'] = $orderId;
$this->orderRepository->update([$data], $context);
}
}