<?php declare(strict_types=1);
namespace Shopware\Core\Content\Flow\Dispatching\Action;
use Doctrine\DBAL\Connection;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity;
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\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Event\CustomerGroupAware;
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 SetCustomerGroupCustomFieldAction extends FlowAction implements DelayableAction
{
use CustomFieldActionTrait;
private Connection $connection;
private EntityRepositoryInterface $customerGroupRepository;
/**
* @internal
*/
public function __construct(
Connection $connection,
EntityRepositoryInterface $customerGroupRepository
) {
$this->connection = $connection;
$this->customerGroupRepository = $customerGroupRepository;
}
public static function getName(): string
{
return 'action.set.customer.group.custom.field';
}
/**
* @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 [CustomerGroupAware::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 CustomerGroupAware) {
return;
}
$this->update($baseEvent->getContext(), $event->getConfig(), $baseEvent->getCustomerGroupId());
}
public function handleFlow(StorableFlow $flow): void
{
if (!$flow->hasStore(CustomerGroupAware::CUSTOMER_GROUP_ID)) {
return;
}
$this->update($flow->getContext(), $flow->getConfig(), $flow->getStore(CustomerGroupAware::CUSTOMER_GROUP_ID));
}
/**
* @param array<string, mixed> $config
*/
private function update(Context $context, array $config, string $customerGroupId): void
{
/** @var CustomerGroupEntity $customerGroup */
$customerGroup = $this->customerGroupRepository->search(new Criteria([$customerGroupId]), $context)->first();
$customFields = $this->getCustomFieldForUpdating($customerGroup->getCustomfields(), $config);
if ($customFields === null) {
return;
}
$customFields = empty($customFields) ? null : $customFields;
$this->customerGroupRepository->update([
[
'id' => $customerGroupId,
'customFields' => $customFields,
],
], $context);
}
}