vendor/shopware/core/Content/Flow/Dispatching/Action/ChangeCustomerGroupAction.php line 61

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Flow\Dispatching\Action;
  3. use Shopware\Core\Content\Flow\Dispatching\DelayableAction;
  4. use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\Event\CustomerAware;
  8. use Shopware\Core\Framework\Event\FlowEvent;
  9. use Shopware\Core\Framework\Feature;
  10. use Shopware\Core\Framework\Log\Package;
  11. /**
  12. * @deprecated tag:v6.5.0 - reason:remove-subscriber - FlowActions won't be executed over the event system anymore,
  13. * therefore the actions won't implement the EventSubscriberInterface anymore.
  14. */
  15. #[Package('business-ops')]
  16. class ChangeCustomerGroupAction extends FlowAction implements DelayableAction
  17. {
  18. private EntityRepositoryInterface $customerRepository;
  19. /**
  20. * @internal
  21. */
  22. public function __construct(EntityRepositoryInterface $customerRepository)
  23. {
  24. $this->customerRepository = $customerRepository;
  25. }
  26. public static function getName(): string
  27. {
  28. return 'action.change.customer.group';
  29. }
  30. /**
  31. * @deprecated tag:v6.5.0 - reason:remove-subscriber - Will be removed
  32. */
  33. public static function getSubscribedEvents(): array
  34. {
  35. if (Feature::isActive('v6.5.0.0')) {
  36. return [];
  37. }
  38. return [
  39. self::getName() => 'handle',
  40. ];
  41. }
  42. /**
  43. * @return array<int, string>
  44. */
  45. public function requirements(): array
  46. {
  47. return [CustomerAware::class];
  48. }
  49. /**
  50. * @deprecated tag:v6.5.0 Will be removed, implement handleFlow instead
  51. */
  52. public function handle(FlowEvent $event): void
  53. {
  54. Feature::triggerDeprecationOrThrow(
  55. 'v6.5.0.0',
  56. Feature::deprecatedMethodMessage(__CLASS__, __METHOD__, 'v6.5.0.0')
  57. );
  58. $baseEvent = $event->getEvent();
  59. if (!$baseEvent instanceof CustomerAware) {
  60. return;
  61. }
  62. $this->update($baseEvent->getContext(), $event->getConfig(), $baseEvent->getCustomerId());
  63. }
  64. public function handleFlow(StorableFlow $flow): void
  65. {
  66. if (!$flow->hasStore(CustomerAware::CUSTOMER_ID)) {
  67. return;
  68. }
  69. $this->update($flow->getContext(), $flow->getConfig(), $flow->getStore(CustomerAware::CUSTOMER_ID));
  70. }
  71. /**
  72. * @param array<string, mixed> $config
  73. */
  74. private function update(Context $context, array $config, string $customerId): void
  75. {
  76. if (!\array_key_exists('customerGroupId', $config)) {
  77. return;
  78. }
  79. $customerGroupId = $config['customerGroupId'];
  80. if (empty($customerGroupId)) {
  81. return;
  82. }
  83. $this->customerRepository->update([
  84. [
  85. 'id' => $customerId,
  86. 'groupId' => $customerGroupId,
  87. ],
  88. ], $context);
  89. }
  90. }