CustomerAddressDataProvider.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\Address;
  8. use Magento\Customer\Api\Data\AddressInterface;
  9. use Magento\Customer\Api\Data\CustomerInterface;
  10. use Magento\Framework\Api\CustomAttributesDataInterface;
  11. use Magento\Customer\Api\AddressRepositoryInterface;
  12. use Magento\Customer\Model\ResourceModel\Customer as CustomerResourceModel;
  13. use Magento\Customer\Model\CustomerFactory;
  14. use Magento\Framework\Webapi\ServiceOutputProcessor;
  15. use Magento\Framework\Serialize\SerializerInterface;
  16. /**
  17. * Customer Address field data provider, used for GraphQL request processing.
  18. */
  19. class CustomerAddressDataProvider
  20. {
  21. /**
  22. * @var ServiceOutputProcessor
  23. */
  24. private $serviceOutputProcessor;
  25. /**
  26. * @var SerializerInterface
  27. */
  28. private $jsonSerializer;
  29. /**
  30. * @var CustomerResourceModel
  31. */
  32. private $customerResourceModel;
  33. /**
  34. * @var CustomerFactory
  35. */
  36. private $customerFactory;
  37. /**
  38. * @param ServiceOutputProcessor $serviceOutputProcessor
  39. * @param SerializerInterface $jsonSerializer
  40. * @param CustomerResourceModel $customerResourceModel
  41. * @param CustomerFactory $customerFactory
  42. */
  43. public function __construct(
  44. ServiceOutputProcessor $serviceOutputProcessor,
  45. SerializerInterface $jsonSerializer,
  46. CustomerResourceModel $customerResourceModel,
  47. CustomerFactory $customerFactory
  48. ) {
  49. $this->serviceOutputProcessor = $serviceOutputProcessor;
  50. $this->jsonSerializer = $jsonSerializer;
  51. $this->customerResourceModel = $customerResourceModel;
  52. $this->customerFactory = $customerFactory;
  53. }
  54. /**
  55. * Curate shipping and billing default options
  56. *
  57. * @param array $address
  58. * @param AddressInterface $addressObject
  59. * @return array
  60. */
  61. private function curateAddressDefaultValues(array $address, AddressInterface $addressObject) : array
  62. {
  63. $customerModel = $this->customerFactory->create();
  64. $this->customerResourceModel->load($customerModel, $addressObject->getCustomerId());
  65. $address[CustomerInterface::DEFAULT_BILLING] =
  66. ($customerModel->getDefaultBillingAddress()
  67. && $addressObject->getId() == $customerModel->getDefaultBillingAddress()->getId());
  68. $address[CustomerInterface::DEFAULT_SHIPPING] =
  69. ($customerModel->getDefaultShippingAddress()
  70. && $addressObject->getId() == $customerModel->getDefaultShippingAddress()->getId());
  71. return $address;
  72. }
  73. /**
  74. * Transform single customer address data from object to in array format
  75. *
  76. * @param AddressInterface $addressObject
  77. * @return array
  78. */
  79. public function getAddressData(AddressInterface $addressObject): array
  80. {
  81. $address = $this->serviceOutputProcessor->process(
  82. $addressObject,
  83. AddressRepositoryInterface::class,
  84. 'getById'
  85. );
  86. $address = $this->curateAddressDefaultValues($address, $addressObject);
  87. if (isset($address[CustomAttributesDataInterface::EXTENSION_ATTRIBUTES_KEY])) {
  88. $address = array_merge($address, $address[CustomAttributesDataInterface::EXTENSION_ATTRIBUTES_KEY]);
  89. }
  90. $customAttributes = [];
  91. if (isset($address[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])) {
  92. foreach ($address[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES] as $attribute) {
  93. $isArray = false;
  94. if (is_array($attribute['value'])) {
  95. $isArray = true;
  96. foreach ($attribute['value'] as $attributeValue) {
  97. if (is_array($attributeValue)) {
  98. $customAttributes[$attribute['attribute_code']] = $this->jsonSerializer->serialize(
  99. $attribute['value']
  100. );
  101. continue;
  102. }
  103. $customAttributes[$attribute['attribute_code']] = implode(',', $attribute['value']);
  104. continue;
  105. }
  106. }
  107. if ($isArray) {
  108. continue;
  109. }
  110. $customAttributes[$attribute['attribute_code']] = $attribute['value'];
  111. }
  112. }
  113. $address = array_merge($address, $customAttributes);
  114. return $address;
  115. }
  116. }