vendor/shopware/core/Content/Flow/Dispatching/Action/AddCustomerAffiliateAndCampaignCodeAction.php line 68

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\Content\Flow\Dispatching\DelayableAction;
  5. use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\Event\CustomerAware;
  9. use Shopware\Core\Framework\Event\FlowEvent;
  10. use Shopware\Core\Framework\Feature;
  11. use Shopware\Core\Framework\Log\Package;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. /**
  14. * @deprecated tag:v6.5.0 - reason:remove-subscriber - FlowActions won't be executed over the event system anymore,
  15. * therefore the actions won't implement the EventSubscriberInterface anymore.
  16. */
  17. #[Package('business-ops')]
  18. class AddCustomerAffiliateAndCampaignCodeAction extends FlowAction implements DelayableAction
  19. {
  20. private Connection $connection;
  21. private EntityRepositoryInterface $customerRepository;
  22. /**
  23. * @internal
  24. */
  25. public function __construct(
  26. Connection $connection,
  27. EntityRepositoryInterface $customerRepository
  28. ) {
  29. $this->connection = $connection;
  30. $this->customerRepository = $customerRepository;
  31. }
  32. public static function getName(): string
  33. {
  34. return 'action.add.customer.affiliate.and.campaign.code';
  35. }
  36. /**
  37. * @deprecated tag:v6.5.0 - reason:remove-subscriber - Will be removed
  38. */
  39. public static function getSubscribedEvents(): array
  40. {
  41. if (Feature::isActive('v6.5.0.0')) {
  42. return [];
  43. }
  44. return [
  45. self::getName() => 'handle',
  46. ];
  47. }
  48. /**
  49. * @return array<int, string>
  50. */
  51. public function requirements(): array
  52. {
  53. return [CustomerAware::class];
  54. }
  55. /**
  56. * @deprecated tag:v6.5.0 Will be removed, implement handleFlow instead
  57. */
  58. public function handle(FlowEvent $event): void
  59. {
  60. Feature::triggerDeprecationOrThrow(
  61. 'v6.5.0.0',
  62. Feature::deprecatedMethodMessage(__CLASS__, __METHOD__, 'v6.5.0.0')
  63. );
  64. $baseEvent = $event->getEvent();
  65. if (!$baseEvent instanceof CustomerAware) {
  66. return;
  67. }
  68. $this->update($baseEvent->getContext(), $event->getConfig(), $baseEvent->getCustomerId());
  69. }
  70. public function handleFlow(StorableFlow $flow): void
  71. {
  72. if (!$flow->hasStore(CustomerAware::CUSTOMER_ID)) {
  73. return;
  74. }
  75. $this->update($flow->getContext(), $flow->getConfig(), $flow->getStore(CustomerAware::CUSTOMER_ID));
  76. }
  77. /**
  78. * @return array<mixed>
  79. */
  80. private function getAffiliateAndCampaignCodeFromCustomerId(string $customerId): array
  81. {
  82. $data = $this->connection->fetchAssociative(
  83. 'SELECT affiliate_code, campaign_code FROM customer WHERE id = :id',
  84. [
  85. 'id' => Uuid::fromHexToBytes($customerId),
  86. ]
  87. );
  88. if (!$data) {
  89. return [];
  90. }
  91. return $data;
  92. }
  93. /**
  94. * @param array<string, mixed> $config
  95. */
  96. private function update(Context $context, array $config, string $customerId): void
  97. {
  98. if (!\array_key_exists('affiliateCode', $config) || !\array_key_exists('campaignCode', $config)) {
  99. return;
  100. }
  101. $customerData = $this->getAffiliateAndCampaignCodeFromCustomerId($customerId);
  102. if (empty($customerData)) {
  103. return;
  104. }
  105. $affiliateCode = $customerData['affiliate_code'];
  106. if ($affiliateCode === null || $config['affiliateCode']['upsert']) {
  107. $affiliateCode = $config['affiliateCode']['value'];
  108. }
  109. $campaignCode = $customerData['campaign_code'];
  110. if ($campaignCode === null || $config['campaignCode']['upsert']) {
  111. $campaignCode = $config['campaignCode']['value'];
  112. }
  113. $data = [];
  114. if ($affiliateCode !== $customerData['affiliate_code']) {
  115. $data['affiliateCode'] = $affiliateCode;
  116. }
  117. if ($campaignCode !== $customerData['campaign_code']) {
  118. $data['campaignCode'] = $campaignCode;
  119. }
  120. if (empty($data)) {
  121. return;
  122. }
  123. $data['id'] = $customerId;
  124. $this->customerRepository->update([$data], $context);
  125. }
  126. }