Skip to content

Commit 7aaba83

Browse files
committed
Refactor fillAddress and add missing doc
1 parent fbefe29 commit 7aaba83

File tree

3 files changed

+39
-118
lines changed

3 files changed

+39
-118
lines changed

app/code/Magento/CustomerGraphQl/Model/Resolver/Address.php

Lines changed: 34 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
declare(strict_types = 1);
6+
declare(strict_types=1);
77

88
namespace Magento\CustomerGraphQl\Model\Resolver;
99

1010
use Magento\Authorization\Model\UserContextInterface;
11-
use Magento\Customer\Api\CustomerRepositoryInterface;
1211
use Magento\Customer\Api\Data\CustomerInterface;
1312
use Magento\Customer\Api\AddressMetadataManagementInterface;
13+
use Magento\Customer\Api\CustomerRepositoryInterface;
1414
use Magento\Customer\Api\AddressRepositoryInterface;
1515
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1616
use Magento\Customer\Api\Data\AddressInterface;
17-
use Magento\Customer\Api\Data\RegionInterfaceFactory;
18-
use Magento\Customer\Api\Data\AddressExtensionInterfaceFactory;
19-
use Magento\Framework\Api\AttributeInterfaceFactory;
17+
use Magento\Framework\Api\DataObjectHelper;
2018
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
2119
use Magento\Framework\GraphQl\Config\Element\Field;
2220
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
23-
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
2421
use Magento\Framework\GraphQl\Query\ResolverInterface;
2522
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
2623
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
@@ -29,7 +26,7 @@
2926
use Magento\Eav\Model\Config;
3027

3128
/**
32-
* Customers Address Update
29+
* Customers Address, used for GraphQL request processing.
3330
*/
3431
class Address implements ResolverInterface
3532
{
@@ -68,10 +65,11 @@ class Address implements ResolverInterface
6865
const MUTATION_ADDRESS_UPDATE = 'customerAddressUpdate';
6966
const MUTATION_ADDRESS_DELETE = 'customerAddressDelete';
7067

68+
7169
/**
7270
* @var CustomerRepositoryInterface
7371
*/
74-
private $customerRepository;
72+
private $customerRepositoryInterface;
7573

7674
/**
7775
* @var AddressRepositoryInterface
@@ -83,21 +81,6 @@ class Address implements ResolverInterface
8381
*/
8482
private $addressInterfaceFactory;
8583

86-
/**
87-
* @var RegionInterfaceFactory
88-
*/
89-
private $regionInterfaceFactory;
90-
91-
/**
92-
* @var AttributeInterfaceFactory
93-
*/
94-
private $attributeInterfaceFactory;
95-
96-
/**
97-
* @var AddressExtensionInterfaceFactory
98-
*/
99-
private $addressExtensionInterfaceFactory;
100-
10184
/**
10285
* @var Config
10386
*/
@@ -109,37 +92,35 @@ class Address implements ResolverInterface
10992
private $addressDataProvider;
11093

11194
/**
112-
* @param CustomerRepositoryInterface $customerRepository
95+
* @var \Magento\Framework\Api\DataObjectHelper
96+
*/
97+
private $dataObjectHelper;
98+
99+
/**
113100
* @param AddressRepositoryInterface $addressRepositoryInterface
114101
* @param AddressInterfaceFactory $addressInterfaceFactory
115-
* @param RegionInterfaceFactory $regionInterfaceFactory
116-
* @param AttributeInterfaceFactory $attributeInterfaceFactory
117-
* @param AddressExtensionInterfaceFactory $addressExtensionInterfaceFactory
118102
* @param Config $eavConfig
119103
* @param AddressDataProvider $addressDataProvider
104+
* @param DataObjectHelper $dataObjectHelper
120105
*/
121106
public function __construct(
122-
CustomerRepositoryInterface $customerRepository,
107+
CustomerRepositoryInterface $customerRepositoryInterface,
123108
AddressRepositoryInterface $addressRepositoryInterface,
124109
AddressInterfaceFactory $addressInterfaceFactory,
125-
RegionInterfaceFactory $regionInterfaceFactory,
126-
AttributeInterfaceFactory $attributeInterfaceFactory,
127-
AddressExtensionInterfaceFactory $addressExtensionInterfaceFactory,
128110
Config $eavConfig,
129-
AddressDataProvider $addressDataProvider
111+
AddressDataProvider $addressDataProvider,
112+
DataObjectHelper $dataObjectHelper
130113
) {
131-
$this->customerRepository = $customerRepository;
114+
$this->customerRepositoryInterface = $customerRepositoryInterface;
132115
$this->addressRepositoryInterface = $addressRepositoryInterface;
133116
$this->addressInterfaceFactory = $addressInterfaceFactory;
134-
$this->regionInterfaceFactory = $regionInterfaceFactory;
135-
$this->attributeInterfaceFactory = $attributeInterfaceFactory;
136-
$this->addressExtensionInterfaceFactory = $addressExtensionInterfaceFactory;
137117
$this->eavConfig = $eavConfig;
138118
$this->addressDataProvider = $addressDataProvider;
119+
$this->dataObjectHelper = $dataObjectHelper;
139120
}
140121

141122
/**
142-
* {@inheritdoc}
123+
* @inheritdoc
143124
*/
144125
public function resolve(
145126
Field $field,
@@ -148,7 +129,7 @@ public function resolve(
148129
array $value = null,
149130
array $args = null
150131
) {
151-
/** @var ContextInterface $context */
132+
/** @var \Magento\Framework\GraphQl\Query\Resolver\ContextInterface $context */
152133
if ((!$context->getUserId()) || $context->getUserType() == UserContextInterface::USER_TYPE_GUEST) {
153134
throw new GraphQlAuthorizationException(
154135
__(
@@ -158,7 +139,7 @@ public function resolve(
158139
);
159140
}
160141
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
161-
$customer = $this->customerRepository->getById($context->getUserId());
142+
$customer = $this->customerRepositoryInterface->getById($context->getUserId());
162143
switch ($field->getName()) {
163144
case self::MUTATION_ADDRESS_CREATE:
164145
return $this->addressDataProvider->processCustomerAddress(
@@ -177,10 +158,10 @@ public function resolve(
177158

178159
/**
179160
* Get input address attribute errors
180-
* @param $addressInput
161+
* @param array $addressInput
181162
* @return bool|string
182163
*/
183-
private function getAddressInputError($addressInput)
164+
private function getAddressInputError(array $addressInput)
184165
{
185166
foreach (self::ADDRESS_ATTRIBUTES as $attribute) {
186167
if ($this->isAttributeRequired($attribute) && !isset($addressInput[$attribute])) {
@@ -192,7 +173,7 @@ private function getAddressInputError($addressInput)
192173

193174
/**
194175
* Check if attribute is set as required
195-
* @param $attributeName
176+
* @param string $attributeName
196177
* @return bool
197178
*/
198179
private function isAttributeRequired($attributeName)
@@ -204,81 +185,18 @@ private function isAttributeRequired($attributeName)
204185
}
205186

206187
/**
188+
* Add $addressInput array information to a $address object
207189
* @param AddressInterface $address
208190
* @param array $addressInput
209191
* @return AddressInterface
210192
*/
211-
private function fillAddress($address, $addressInput)
193+
private function fillAddress(AddressInterface $address, array $addressInput) : AddressInterface
212194
{
213-
if (isset($addressInput[AddressInterface::REGION])) {
214-
/** @var \Magento\Customer\Api\Data\RegionInterface $newRegion */
215-
$newRegion = $this->regionInterfaceFactory->create($addressInput[AddressInterface::REGION]);
216-
$address->setRegion($newRegion);
217-
}
218-
if (isset($addressInput[AddressInterface::REGION_ID])) {
219-
$address->setRegionId($addressInput[AddressInterface::REGION_ID]);
220-
}
221-
if (isset($addressInput[AddressInterface::COUNTRY_ID])) {
222-
$address->setCountryId($addressInput[AddressInterface::COUNTRY_ID]);
223-
}
224-
if (isset($addressInput[AddressInterface::STREET])) {
225-
$address->setStreet($addressInput[AddressInterface::STREET]);
226-
}
227-
if (isset($addressInput[AddressInterface::COMPANY])) {
228-
$address->setCompany($addressInput[AddressInterface::COMPANY]);
229-
}
230-
if (isset($addressInput[AddressInterface::TELEPHONE])) {
231-
$address->setTelephone($addressInput[AddressInterface::TELEPHONE]);
232-
}
233-
if (isset($addressInput[AddressInterface::FAX])) {
234-
$address->setFax($addressInput[AddressInterface::FAX]);
235-
}
236-
if (isset($addressInput[AddressInterface::POSTCODE])) {
237-
$address->setPostcode($addressInput[AddressInterface::POSTCODE]);
238-
}
239-
if (isset($addressInput[AddressInterface::CITY])) {
240-
$address->setCity($addressInput[AddressInterface::CITY]);
241-
}
242-
if (isset($addressInput[AddressInterface::FIRSTNAME])) {
243-
$address->setFirstname($addressInput[AddressInterface::FIRSTNAME]);
244-
}
245-
if (isset($addressInput[AddressInterface::LASTNAME])) {
246-
$address->setLastname($addressInput[AddressInterface::LASTNAME]);
247-
}
248-
if (isset($addressInput[AddressInterface::MIDDLENAME])) {
249-
$address->setMiddlename($addressInput[AddressInterface::MIDDLENAME]);
250-
}
251-
if (isset($addressInput[AddressInterface::PREFIX])) {
252-
$address->setPrefix($addressInput[AddressInterface::PREFIX]);
253-
}
254-
if (isset($addressInput[AddressInterface::SUFFIX])) {
255-
$address->setSuffix($addressInput[AddressInterface::SUFFIX]);
256-
}
257-
if (isset($addressInput[AddressInterface::VAT_ID])) {
258-
$address->setVatId($addressInput[AddressInterface::VAT_ID]);
259-
}
260-
if (isset($addressInput[AddressInterface::DEFAULT_BILLING])) {
261-
$address->setIsDefaultBilling((bool)$addressInput[AddressInterface::DEFAULT_BILLING]);
262-
}
263-
if (isset($addressInput[AddressInterface::DEFAULT_SHIPPING])) {
264-
$address->setIsDefaultShipping((bool)$addressInput[AddressInterface::DEFAULT_SHIPPING]);
265-
}
266-
if (isset($addressInput[self::CUSTOM_ATTRIBUTE_KEY])) {
267-
foreach ($addressInput[self::CUSTOM_ATTRIBUTE_KEY] as $attribute) {
268-
$address->setCustomAttribute($attribute['attribute_code'], $attribute['value']);
269-
}
270-
}
271-
if (isset($addressInput[self::EXTENSION_ATTRIBUTE_KEY])) {
272-
$extensionAttributes = $address->getExtensionAttributes();
273-
if (!$extensionAttributes) {
274-
/** @var \Magento\Customer\Api\Data\AddressExtensionInterface $newExtensionAttribute */
275-
$extensionAttributes = $this->addressExtensionInterfaceFactory->create();
276-
}
277-
foreach ($addressInput[self::EXTENSION_ATTRIBUTE_KEY] as $attribute) {
278-
$extensionAttributes->setData($attribute['attribute_code'], $attribute['value']);
279-
}
280-
$address->setExtensionAttributes($extensionAttributes);
281-
}
195+
$this->dataObjectHelper->populateWithArray(
196+
$address,
197+
$addressInput,
198+
\Magento\Customer\Api\Data\AddressInterface::class
199+
);
282200
return $address;
283201
}
284202

@@ -289,7 +207,7 @@ private function fillAddress($address, $addressInput)
289207
* @return AddressInterface
290208
* @throws GraphQlInputException
291209
*/
292-
private function processCustomerAddressCreate($customer, $addressInput)
210+
private function processCustomerAddressCreate(CustomerInterface $customer, array $addressInput) : AddressInterface
293211
{
294212
$errorInput = $this->getAddressInputError($addressInput);
295213
if ($errorInput) {
@@ -313,7 +231,7 @@ private function processCustomerAddressCreate($customer, $addressInput)
313231
* @throws GraphQlAuthorizationException
314232
* @throws GraphQlNoSuchEntityException
315233
*/
316-
private function processCustomerAddressUpdate($customer, $addressId, $addressInput)
234+
private function processCustomerAddressUpdate(CustomerInterface $customer, $addressId, array $addressInput)
317235
{
318236
try {
319237
/** @var AddressInterface $address */
@@ -341,7 +259,7 @@ private function processCustomerAddressUpdate($customer, $addressId, $addressInp
341259
* @throws GraphQlAuthorizationException
342260
* @throws GraphQlNoSuchEntityException
343261
*/
344-
private function processCustomerAddressDelete($customer, $addressId)
262+
private function processCustomerAddressDelete(CustomerInterface $customer, $addressId)
345263
{
346264
try {
347265
/** @var AddressInterface $address */
@@ -368,4 +286,4 @@ private function processCustomerAddressDelete($customer, $addressId)
368286
}
369287
return $this->addressRepositoryInterface->delete($address);
370288
}
371-
}
289+
}

app/code/Magento/CustomerGraphQl/Model/Resolver/Address/AddressDataProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use Magento\Framework\Webapi\ServiceOutputProcessor;
1313
use Magento\Framework\Serialize\SerializerInterface;
1414

15+
/**
16+
* Customer Address field data provider, used for GraphQL request processing.
17+
*/
1518
class AddressDataProvider
1619
{
1720
/**
@@ -79,4 +82,4 @@ public function processCustomerAddress(AddressInterface $addressObject) : array
7982

8083
return $address;
8184
}
82-
}
85+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CustomerAddressTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
declare(strict_types = 1);
6+
declare(strict_types=1);
77

88
namespace Magento\GraphQl\Customer;
99

0 commit comments

Comments
 (0)