Storage.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Customer\Model\Delegation;
  8. use Magento\Customer\Api\Data\AddressInterface;
  9. use Magento\Customer\Api\Data\CustomerInterface;
  10. use Magento\Customer\Api\Data\RegionInterface;
  11. use Magento\Customer\Api\Data\RegionInterfaceFactory;
  12. use Magento\Customer\Model\Delegation\Data\NewOperation;
  13. use Magento\Customer\Model\Data\Customer;
  14. use Magento\Customer\Model\Data\Address;
  15. use Magento\Customer\Model\Session;
  16. use Magento\Customer\Model\Session\Proxy as SessionProxy;
  17. use Magento\Customer\Model\Delegation\Data\NewOperationFactory;
  18. use Magento\Customer\Api\Data\CustomerInterfaceFactory;
  19. use Magento\Customer\Api\Data\AddressInterfaceFactory;
  20. use Psr\Log\LoggerInterface;
  21. /**
  22. * Store data for delegated operations.
  23. */
  24. class Storage
  25. {
  26. /**
  27. * @var Session
  28. */
  29. private $session;
  30. /**
  31. * @var NewOperationFactory
  32. */
  33. private $newFactory;
  34. /**
  35. * @var CustomerInterfaceFactory
  36. */
  37. private $customerFactory;
  38. /**
  39. * @var AddressInterfaceFactory
  40. */
  41. private $addressFactory;
  42. /**
  43. * @var RegionInterfaceFactory
  44. */
  45. private $regionFactory;
  46. /**
  47. * @var LoggerInterface
  48. */
  49. private $logger;
  50. /**
  51. * @param NewOperationFactory $newFactory
  52. * @param CustomerInterfaceFactory $customerFactory
  53. * @param AddressInterfaceFactory $addressFactory
  54. * @param RegionInterfaceFactory $regionFactory
  55. * @param LoggerInterface $logger
  56. * @param SessionProxy $session
  57. */
  58. public function __construct(
  59. NewOperationFactory $newFactory,
  60. CustomerInterfaceFactory $customerFactory,
  61. AddressInterfaceFactory $addressFactory,
  62. RegionInterfaceFactory $regionFactory,
  63. LoggerInterface $logger,
  64. SessionProxy $session
  65. ) {
  66. $this->newFactory = $newFactory;
  67. $this->customerFactory = $customerFactory;
  68. $this->addressFactory = $addressFactory;
  69. $this->regionFactory = $regionFactory;
  70. $this->logger = $logger;
  71. $this->session = $session;
  72. }
  73. /**
  74. * Store data for new account operation.
  75. *
  76. * @param CustomerInterface $customer
  77. * @param array $delegatedData
  78. *
  79. * @return void
  80. */
  81. public function storeNewOperation(CustomerInterface $customer, array $delegatedData): void
  82. {
  83. /** @var Customer $customer */
  84. $customerData = $customer->__toArray();
  85. $addressesData = [];
  86. if ($customer->getAddresses()) {
  87. /** @var Address $address */
  88. foreach ($customer->getAddresses() as $address) {
  89. $addressesData[] = $address->__toArray();
  90. }
  91. }
  92. $this->session->setCustomerFormData($customerData);
  93. $this->session->setDelegatedNewCustomerData([
  94. 'customer' => $customerData,
  95. 'addresses' => $addressesData,
  96. 'delegated_data' => $delegatedData,
  97. ]);
  98. }
  99. /**
  100. * Retrieve delegated new operation data and mark it as used.
  101. *
  102. * @return NewOperation|null
  103. */
  104. public function consumeNewOperation()
  105. {
  106. try {
  107. $serialized = $this->session->getDelegatedNewCustomerData(true);
  108. } catch (\Throwable $exception) {
  109. $this->logger->error($exception);
  110. $serialized = null;
  111. }
  112. if ($serialized === null) {
  113. return null;
  114. }
  115. /** @var AddressInterface[] $addresses */
  116. $addresses = [];
  117. foreach ($serialized['addresses'] as $addressData) {
  118. if (isset($addressData['region'])) {
  119. /** @var RegionInterface $region */
  120. $region = $this->regionFactory->create(
  121. ['data' => $addressData['region']]
  122. );
  123. $addressData['region'] = $region;
  124. }
  125. $addresses[] = $this->addressFactory->create(
  126. ['data' => $addressData]
  127. );
  128. }
  129. $customerData = $serialized['customer'];
  130. $customerData['addresses'] = $addresses;
  131. return $this->newFactory->create([
  132. 'customer' => $this->customerFactory->create(
  133. ['data' => $customerData]
  134. ),
  135. 'additionalData' => $serialized['delegated_data'],
  136. ]);
  137. }
  138. }