vendor/shopware/core/Content/Flow/Dispatching/Action/SetCustomerGroupCustomFieldAction.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\Aggregate\CustomerGroup\CustomerGroupEntity;
  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\CustomerGroupAware;
  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 SetCustomerGroupCustomFieldAction extends FlowAction implements DelayableAction
  20. {
  21. use CustomFieldActionTrait;
  22. private Connection $connection;
  23. private EntityRepositoryInterface $customerGroupRepository;
  24. /**
  25. * @internal
  26. */
  27. public function __construct(
  28. Connection $connection,
  29. EntityRepositoryInterface $customerGroupRepository
  30. ) {
  31. $this->connection = $connection;
  32. $this->customerGroupRepository = $customerGroupRepository;
  33. }
  34. public static function getName(): string
  35. {
  36. return 'action.set.customer.group.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 [CustomerGroupAware::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 CustomerGroupAware) {
  68. return;
  69. }
  70. $this->update($baseEvent->getContext(), $event->getConfig(), $baseEvent->getCustomerGroupId());
  71. }
  72. public function handleFlow(StorableFlow $flow): void
  73. {
  74. if (!$flow->hasStore(CustomerGroupAware::CUSTOMER_GROUP_ID)) {
  75. return;
  76. }
  77. $this->update($flow->getContext(), $flow->getConfig(), $flow->getStore(CustomerGroupAware::CUSTOMER_GROUP_ID));
  78. }
  79. /**
  80. * @param array<string, mixed> $config
  81. */
  82. private function update(Context $context, array $config, string $customerGroupId): void
  83. {
  84. /** @var CustomerGroupEntity $customerGroup */
  85. $customerGroup = $this->customerGroupRepository->search(new Criteria([$customerGroupId]), $context)->first();
  86. $customFields = $this->getCustomFieldForUpdating($customerGroup->getCustomfields(), $config);
  87. if ($customFields === null) {
  88. return;
  89. }
  90. $customFields = empty($customFields) ? null : $customFields;
  91. $this->customerGroupRepository->update([
  92. [
  93. 'id' => $customerGroupId,
  94. 'customFields' => $customFields,
  95. ],
  96. ], $context);
  97. }
  98. }