|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CustomerGraphQl\Model\Customer\Address; |
| 9 | + |
| 10 | +use Magento\Customer\Api\Data\AddressInterface; |
| 11 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 12 | +use Magento\Framework\Api\CustomAttributesDataInterface; |
| 13 | +use Magento\Customer\Api\AddressRepositoryInterface; |
| 14 | +use Magento\Customer\Model\ResourceModel\Customer as CustomerResourceModel; |
| 15 | +use Magento\Customer\Model\CustomerFactory; |
| 16 | +use Magento\Framework\Webapi\ServiceOutputProcessor; |
| 17 | +use Magento\Framework\Serialize\SerializerInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * Customer Address field data provider, used for GraphQL request processing. |
| 21 | + */ |
| 22 | +class CustomerAddressDataProvider |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var ServiceOutputProcessor |
| 26 | + */ |
| 27 | + private $serviceOutputProcessor; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var SerializerInterface |
| 31 | + */ |
| 32 | + private $jsonSerializer; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var CustomerResourceModel |
| 36 | + */ |
| 37 | + private $customerResourceModel; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var CustomerFactory |
| 41 | + */ |
| 42 | + private $customerFactory; |
| 43 | + |
| 44 | + /** |
| 45 | + * @param ServiceOutputProcessor $serviceOutputProcessor |
| 46 | + * @param SerializerInterface $jsonSerializer |
| 47 | + * @param CustomerResourceModel $customerResourceModel |
| 48 | + * @param CustomerFactory $customerFactory |
| 49 | + */ |
| 50 | + public function __construct( |
| 51 | + ServiceOutputProcessor $serviceOutputProcessor, |
| 52 | + SerializerInterface $jsonSerializer, |
| 53 | + CustomerResourceModel $customerResourceModel, |
| 54 | + CustomerFactory $customerFactory |
| 55 | + ) { |
| 56 | + $this->serviceOutputProcessor = $serviceOutputProcessor; |
| 57 | + $this->jsonSerializer = $jsonSerializer; |
| 58 | + $this->customerResourceModel = $customerResourceModel; |
| 59 | + $this->customerFactory = $customerFactory; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Curate shipping and billing default options |
| 64 | + * |
| 65 | + * @param array $address |
| 66 | + * @param AddressInterface $addressObject |
| 67 | + * @return array |
| 68 | + */ |
| 69 | + private function curateAddressDefaultValues(array $address, AddressInterface $addressObject) : array |
| 70 | + { |
| 71 | + $customerModel = $this->customerFactory->create(); |
| 72 | + $this->customerResourceModel->load($customerModel, $addressObject->getCustomerId()); |
| 73 | + $address[CustomerInterface::DEFAULT_BILLING] = |
| 74 | + ($customerModel->getDefaultBillingAddress() |
| 75 | + && $addressObject->getId() == $customerModel->getDefaultBillingAddress()->getId()); |
| 76 | + $address[CustomerInterface::DEFAULT_SHIPPING] = |
| 77 | + ($customerModel->getDefaultShippingAddress() |
| 78 | + && $addressObject->getId() == $customerModel->getDefaultShippingAddress()->getId()); |
| 79 | + return $address; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Transform single customer address data from object to in array format |
| 84 | + * |
| 85 | + * @param AddressInterface $addressObject |
| 86 | + * @return array |
| 87 | + */ |
| 88 | + public function getAddressData(AddressInterface $addressObject): array |
| 89 | + { |
| 90 | + $address = $this->serviceOutputProcessor->process( |
| 91 | + $addressObject, |
| 92 | + AddressRepositoryInterface::class, |
| 93 | + 'getById' |
| 94 | + ); |
| 95 | + $address = $this->curateAddressDefaultValues($address, $addressObject); |
| 96 | + |
| 97 | + if (isset($address[CustomAttributesDataInterface::EXTENSION_ATTRIBUTES_KEY])) { |
| 98 | + $address = array_merge($address, $address[CustomAttributesDataInterface::EXTENSION_ATTRIBUTES_KEY]); |
| 99 | + } |
| 100 | + $customAttributes = []; |
| 101 | + if (isset($address[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])) { |
| 102 | + foreach ($address[CustomAttributesDataInterface::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->jsonSerializer->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 | + $address = array_merge($address, $customAttributes); |
| 124 | + |
| 125 | + return $address; |
| 126 | + } |
| 127 | +} |
0 commit comments