CustomerDataProvider.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\CustomerGraphQl\Model\Customer;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
  12. use Magento\Framework\Serialize\SerializerInterface;
  13. use Magento\Framework\Webapi\ServiceOutputProcessor;
  14. use Magento\Customer\Api\Data\CustomerInterface;
  15. /**
  16. * Customer field data provider, used for GraphQL request processing.
  17. */
  18. class CustomerDataProvider
  19. {
  20. /**
  21. * @var CustomerRepositoryInterface
  22. */
  23. private $customerRepository;
  24. /**
  25. * @var ServiceOutputProcessor
  26. */
  27. private $serviceOutputProcessor;
  28. /**
  29. * @var SerializerInterface
  30. */
  31. private $serializer;
  32. /**
  33. * @param CustomerRepositoryInterface $customerRepository
  34. * @param ServiceOutputProcessor $serviceOutputProcessor
  35. * @param SerializerInterface $serializer
  36. */
  37. public function __construct(
  38. CustomerRepositoryInterface $customerRepository,
  39. ServiceOutputProcessor $serviceOutputProcessor,
  40. SerializerInterface $serializer
  41. ) {
  42. $this->customerRepository = $customerRepository;
  43. $this->serviceOutputProcessor = $serviceOutputProcessor;
  44. $this->serializer = $serializer;
  45. }
  46. /**
  47. * Get customer data by Id or empty array
  48. *
  49. * @param int $customerId
  50. * @return array
  51. * @throws NoSuchEntityException|LocalizedException
  52. */
  53. public function getCustomerById(int $customerId): array
  54. {
  55. try {
  56. $customer = $this->customerRepository->getById($customerId);
  57. } catch (NoSuchEntityException $e) {
  58. throw new GraphQlNoSuchEntityException(
  59. __('Customer id "%customer_id" does not exist.', ['customer_id' => $customerId]),
  60. $e
  61. );
  62. }
  63. return $this->processCustomer($customer);
  64. }
  65. /**
  66. * Curate default shipping and default billing keys
  67. *
  68. * @param array $arrayAddress
  69. * @return array
  70. */
  71. private function curateAddressData(array $arrayAddress) : array
  72. {
  73. foreach ($arrayAddress as $key => $address) {
  74. if (!isset($address['default_shipping'])) {
  75. $arrayAddress[$key]['default_shipping'] = false;
  76. }
  77. if (!isset($address['default_billing'])) {
  78. $arrayAddress[$key]['default_billing'] = false;
  79. }
  80. }
  81. return $arrayAddress;
  82. }
  83. /**
  84. * Transform single customer data from object to in array format
  85. *
  86. * @param CustomerInterface $customer
  87. * @return array
  88. */
  89. private function processCustomer(CustomerInterface $customer): array
  90. {
  91. $customerData = $this->serviceOutputProcessor->process(
  92. $customer,
  93. CustomerRepositoryInterface::class,
  94. 'get'
  95. );
  96. $customerData['addresses'] = $this->curateAddressData($customerData['addresses']);
  97. if (isset($customerData['extension_attributes'])) {
  98. $customerData = array_merge($customerData, $customerData['extension_attributes']);
  99. }
  100. $customAttributes = [];
  101. if (isset($customerData['custom_attributes'])) {
  102. foreach ($customerData['custom_attributes'] as $attribute) {
  103. $isArray = false;
  104. if (is_array($attribute['value'])) {
  105. $isArray = true;
  106. foreach ($attribute['value'] as $attributeValue) {
  107. if (is_array($attributeValue)) {
  108. $customAttributes[$attribute['attribute_code']] = $this->serializer->serialize(
  109. $attribute['value']
  110. );
  111. continue;
  112. }
  113. $customAttributes[$attribute['attribute_code']] = implode(',', $attribute['value']);
  114. continue;
  115. }
  116. }
  117. if ($isArray) {
  118. continue;
  119. }
  120. $customAttributes[$attribute['attribute_code']] = $attribute['value'];
  121. }
  122. }
  123. $customerData = array_merge($customerData, $customAttributes);
  124. return $customerData;
  125. }
  126. }