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

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