vendor/shopware/core/Content/Flow/Dispatching/Action/SetCustomerCustomFieldAction.php line 71

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\CustomerEntity;
  5. use Shopware\Core\Content\Flow\Dispatching\DelayableAction;
  6. use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\Event\CustomerAware;
  11. use Shopware\Core\Framework\Event\FlowEvent;
  12. use Shopware\Core\Framework\Feature;
  13. use Shopware\Core\Framework\Log\Package;
  14. /**
  15.  * @deprecated tag:v6.5.0 - reason:remove-subscriber - FlowActions won't be executed over the event system anymore,
  16.  * therefore the actions won't implement the EventSubscriberInterface anymore.
  17.  */
  18. #[Package('business-ops')]
  19. class SetCustomerCustomFieldAction extends FlowAction implements DelayableAction
  20. {
  21.     use CustomFieldActionTrait;
  22.     private Connection $connection;
  23.     private EntityRepositoryInterface $customerRepository;
  24.     /**
  25.      * @internal
  26.      */
  27.     public function __construct(
  28.         Connection $connection,
  29.         EntityRepositoryInterface $customerRepository
  30.     ) {
  31.         $this->connection $connection;
  32.         $this->customerRepository $customerRepository;
  33.     }
  34.     public static function getName(): string
  35.     {
  36.         return 'action.set.customer.custom.field';
  37.     }
  38.     /**
  39.      * @deprecated tag:v6.5.0 - reason:remove-subscriber - Will be removed
  40.      */
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         if (Feature::isActive('v6.5.0.0')) {
  44.             return [];
  45.         }
  46.         return [
  47.             self::getName() => 'handle',
  48.         ];
  49.     }
  50.     /**
  51.      * @return array<int, string>
  52.      */
  53.     public function requirements(): array
  54.     {
  55.         return [CustomerAware::class];
  56.     }
  57.     /**
  58.      * @deprecated tag:v6.5.0 Will be removed, implement handleFlow instead
  59.      */
  60.     public function handle(FlowEvent $event): void
  61.     {
  62.         Feature::triggerDeprecationOrThrow(
  63.             'v6.5.0.0',
  64.             Feature::deprecatedMethodMessage(__CLASS____METHOD__'v6.5.0.0')
  65.         );
  66.         $baseEvent $event->getEvent();
  67.         if (!$baseEvent instanceof CustomerAware) {
  68.             return;
  69.         }
  70.         $this->update($baseEvent->getContext(), $event->getConfig(), $baseEvent->getCustomerId());
  71.     }
  72.     public function handleFlow(StorableFlow $flow): void
  73.     {
  74.         if (!$flow->hasStore(CustomerAware::CUSTOMER_ID)) {
  75.             return;
  76.         }
  77.         $this->update($flow->getContext(), $flow->getConfig(), $flow->getStore(CustomerAware::CUSTOMER_ID));
  78.     }
  79.     /**
  80.      * @param array<string, mixed> $config
  81.      */
  82.     private function update(Context $context, array $configstring $customerId): void
  83.     {
  84.         /** @var CustomerEntity $customer */
  85.         $customer $this->customerRepository->search(new Criteria([$customerId]), $context)->first();
  86.         $customFields $this->getCustomFieldForUpdating($customer->getCustomfields(), $config);
  87.         if ($customFields === null) {
  88.             return;
  89.         }
  90.         $customFields = empty($customFields) ? null $customFields;
  91.         $this->customerRepository->update([
  92.             [
  93.                 'id' => $customerId,
  94.                 'customFields' => $customFields,
  95.             ],
  96.         ], $context);
  97.     }
  98. }