vendor/shopware/core/Content/Flow/Dispatching/Action/SetCustomerGroupCustomFieldAction.php line 49

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\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\Event\CustomerGroupAware;
  8. use Shopware\Core\Framework\Event\DelayAware;
  9. use Shopware\Core\Framework\Event\FlowEvent;
  10. class SetCustomerGroupCustomFieldAction extends FlowAction
  11. {
  12.     use CustomFieldActionTrait;
  13.     private Connection $connection;
  14.     private EntityRepositoryInterface $customerGroupRepository;
  15.     /**
  16.      * @internal
  17.      */
  18.     public function __construct(
  19.         Connection $connection,
  20.         EntityRepositoryInterface $customerGroupRepository
  21.     ) {
  22.         $this->connection $connection;
  23.         $this->customerGroupRepository $customerGroupRepository;
  24.     }
  25.     public static function getName(): string
  26.     {
  27.         return 'action.set.customer.group.custom.field';
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             self::getName() => 'handle',
  33.         ];
  34.     }
  35.     public function requirements(): array
  36.     {
  37.         return [CustomerGroupAware::class, DelayAware::class];
  38.     }
  39.     public function handle(FlowEvent $event): void
  40.     {
  41.         $baseEvent $event->getEvent();
  42.         if (!$baseEvent instanceof CustomerGroupAware) {
  43.             return;
  44.         }
  45.         $config $event->getConfig();
  46.         $customerGroupId $baseEvent->getCustomerGroupId();
  47.         /** @var CustomerGroupEntity $customerGroup */
  48.         $customerGroup $this->customerGroupRepository->search(new Criteria([$customerGroupId]), $baseEvent->getContext())->first();
  49.         $customFields $this->getCustomFieldForUpdating($customerGroup->getCustomfields(), $config);
  50.         if ($customFields === null) {
  51.             return;
  52.         }
  53.         $customFields = empty($customFields) ? null $customFields;
  54.         $this->customerGroupRepository->update([
  55.             [
  56.                 'id' => $customerGroupId,
  57.                 'customFields' => $customFields,
  58.             ],
  59.         ], $baseEvent->getContext());
  60.     }
  61. }