<?php declare(strict_types=1);
namespace Shopware\Core\Content\Flow\Dispatching\Action;
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\CustomerAware;
use Shopware\Core\Framework\Event\FlowEvent;
use Shopware\Core\Framework\Feature;
use Shopware\Core\Framework\Log\Package;
/**
* @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 RemoveCustomerTagAction extends FlowAction implements DelayableAction
{
private EntityRepositoryInterface $customerTagRepository;
/**
* @internal
*/
public function __construct(EntityRepositoryInterface $customerTagRepository)
{
$this->customerTagRepository = $customerTagRepository;
}
public static function getName(): string
{
return 'action.remove.customer.tag';
}
/**
* @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 [CustomerAware::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 CustomerAware) {
return;
}
$this->update($baseEvent->getContext(), $event->getConfig(), $baseEvent->getCustomerId());
}
public function handleFlow(StorableFlow $flow): void
{
if (!$flow->hasStore(CustomerAware::CUSTOMER_ID)) {
return;
}
$this->update($flow->getContext(), $flow->getConfig(), $flow->getStore(CustomerAware::CUSTOMER_ID));
}
/**
* @param array<string, mixed> $config
*/
private function update(Context $context, array $config, string $customerId): void
{
if (!\array_key_exists('tagIds', $config)) {
return;
}
$tagIds = array_keys($config['tagIds']);
if (empty($tagIds)) {
return;
}
$tags = array_map(static function ($tagId) use ($customerId) {
return [
'customerId' => $customerId,
'tagId' => $tagId,
];
}, $tagIds);
$this->customerTagRepository->delete($tags, $context);
}
}