Skip to content

Commit 47adffd

Browse files
committed
GraphQL-57: Refactor address resolve to check address attributes required dinamically
1 parent 7aaba83 commit 47adffd

File tree

1 file changed

+30
-60
lines changed
  • app/code/Magento/CustomerGraphQl/Model/Resolver

1 file changed

+30
-60
lines changed

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

Lines changed: 30 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,14 @@
99

1010
use Magento\Authorization\Model\UserContextInterface;
1111
use Magento\Customer\Api\Data\CustomerInterface;
12-
use Magento\Customer\Api\AddressMetadataManagementInterface;
1312
use Magento\Customer\Api\CustomerRepositoryInterface;
1413
use Magento\Customer\Api\AddressRepositoryInterface;
1514
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1615
use Magento\Customer\Api\Data\AddressInterface;
1716
use Magento\Framework\Api\DataObjectHelper;
1817
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1918
use Magento\Framework\GraphQl\Config\Element\Field;
20-
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
2119
use Magento\Framework\GraphQl\Query\ResolverInterface;
22-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
23-
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
24-
use Magento\Framework\Exception\NoSuchEntityException;
2520
use Magento\CustomerGraphQl\Model\Resolver\Address\AddressDataProvider;
2621
use Magento\Eav\Model\Config;
2722

@@ -30,28 +25,6 @@
3025
*/
3126
class Address implements ResolverInterface
3227
{
33-
/**
34-
* Customer address attributes
35-
* @var array
36-
*/
37-
const ADDRESS_ATTRIBUTES = [
38-
AddressInterface::REGION,
39-
AddressInterface::REGION_ID,
40-
AddressInterface::COUNTRY_ID,
41-
AddressInterface::STREET,
42-
AddressInterface::COMPANY,
43-
AddressInterface::TELEPHONE,
44-
AddressInterface::FAX,
45-
AddressInterface::POSTCODE,
46-
AddressInterface::CITY,
47-
AddressInterface::FIRSTNAME,
48-
AddressInterface::LASTNAME,
49-
AddressInterface::MIDDLENAME,
50-
AddressInterface::PREFIX,
51-
AddressInterface::SUFFIX,
52-
AddressInterface::VAT_ID
53-
];
54-
5528
/**
5629
* Input data key
5730
*/
@@ -65,7 +38,6 @@ class Address implements ResolverInterface
6538
const MUTATION_ADDRESS_UPDATE = 'customerAddressUpdate';
6639
const MUTATION_ADDRESS_DELETE = 'customerAddressDelete';
6740

68-
6941
/**
7042
* @var CustomerRepositoryInterface
7143
*/
@@ -97,6 +69,12 @@ class Address implements ResolverInterface
9769
private $dataObjectHelper;
9870

9971
/**
72+
* @var array
73+
*/
74+
private $addressAttributes;
75+
76+
/**
77+
* @param CustomerRepositoryInterface $customerRepositoryInterface
10078
* @param AddressRepositoryInterface $addressRepositoryInterface
10179
* @param AddressInterfaceFactory $addressInterfaceFactory
10280
* @param Config $eavConfig
@@ -117,6 +95,9 @@ public function __construct(
11795
$this->eavConfig = $eavConfig;
11896
$this->addressDataProvider = $addressDataProvider;
11997
$this->dataObjectHelper = $dataObjectHelper;
98+
$this->addressAttributes = $this->eavConfig->getEntityAttributes(
99+
\Magento\Customer\Api\AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS
100+
);
120101
}
121102

122103
/**
@@ -131,7 +112,7 @@ public function resolve(
131112
) {
132113
/** @var \Magento\Framework\GraphQl\Query\Resolver\ContextInterface $context */
133114
if ((!$context->getUserId()) || $context->getUserType() == UserContextInterface::USER_TYPE_GUEST) {
134-
throw new GraphQlAuthorizationException(
115+
throw new \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException(
135116
__(
136117
'Current customer does not have access to the resource "%1"',
137118
[\Magento\Customer\Model\Customer::ENTITY]
@@ -163,27 +144,14 @@ public function resolve(
163144
*/
164145
private function getAddressInputError(array $addressInput)
165146
{
166-
foreach (self::ADDRESS_ATTRIBUTES as $attribute) {
167-
if ($this->isAttributeRequired($attribute) && !isset($addressInput[$attribute])) {
168-
return $attribute;
147+
foreach ($this->addressAttributes as $attributeName => $attributeInfo) {
148+
if ($attributeInfo->getIsRequired() && !isset($addressInput[$attributeName])) {
149+
return $attributeName;
169150
}
170151
}
171152
return false;
172153
}
173154

174-
/**
175-
* Check if attribute is set as required
176-
* @param string $attributeName
177-
* @return bool
178-
*/
179-
private function isAttributeRequired($attributeName)
180-
{
181-
return $this->eavConfig->getAttribute(
182-
AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS,
183-
$attributeName
184-
)->getIsRequired();
185-
}
186-
187155
/**
188156
* Add $addressInput array information to a $address object
189157
* @param AddressInterface $address
@@ -195,7 +163,7 @@ private function fillAddress(AddressInterface $address, array $addressInput) : A
195163
$this->dataObjectHelper->populateWithArray(
196164
$address,
197165
$addressInput,
198-
\Magento\Customer\Api\Data\AddressInterface::class
166+
AddressInterface::class
199167
);
200168
return $address;
201169
}
@@ -205,13 +173,15 @@ private function fillAddress(AddressInterface $address, array $addressInput) : A
205173
* @param CustomerInterface $customer
206174
* @param array $addressInput
207175
* @return AddressInterface
208-
* @throws GraphQlInputException
176+
* @throws \Magento\Framework\GraphQl\Exception\GraphQlInputException
209177
*/
210178
private function processCustomerAddressCreate(CustomerInterface $customer, array $addressInput) : AddressInterface
211179
{
212180
$errorInput = $this->getAddressInputError($addressInput);
213181
if ($errorInput) {
214-
throw new GraphQlInputException(__('Required parameter %1 is missing', [$errorInput]));
182+
throw new \Magento\Framework\GraphQl\Exception\GraphQlInputException(
183+
__('Required parameter %1 is missing', [$errorInput])
184+
);
215185
}
216186
/** @var AddressInterface $newAddress */
217187
$newAddress = $this->fillAddress(
@@ -228,21 +198,21 @@ private function processCustomerAddressCreate(CustomerInterface $customer, array
228198
* @param int $addressId
229199
* @param array $addressInput
230200
* @return AddressInterface
231-
* @throws GraphQlAuthorizationException
232-
* @throws GraphQlNoSuchEntityException
201+
* @throws \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException
202+
* @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException
233203
*/
234204
private function processCustomerAddressUpdate(CustomerInterface $customer, $addressId, array $addressInput)
235205
{
236206
try {
237207
/** @var AddressInterface $address */
238208
$address = $this->addressRepositoryInterface->getById($addressId);
239-
} catch (NoSuchEntityException $exception) {
240-
throw new GraphQlNoSuchEntityException(
209+
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
210+
throw new \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException(
241211
__('Address id %1 does not exist.', [$addressId])
242212
);
243213
}
244214
if ($address->getCustomerId() != $customer->getId()) {
245-
throw new GraphQlAuthorizationException(
215+
throw new \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException(
246216
__('Current customer does not have permission to update address id %1', [$addressId])
247217
);
248218
}
@@ -256,31 +226,31 @@ private function processCustomerAddressUpdate(CustomerInterface $customer, $addr
256226
* @param CustomerInterface $customer
257227
* @param int $addressId
258228
* @return bool
259-
* @throws GraphQlAuthorizationException
260-
* @throws GraphQlNoSuchEntityException
229+
* @throws \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException
230+
* @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException
261231
*/
262232
private function processCustomerAddressDelete(CustomerInterface $customer, $addressId)
263233
{
264234
try {
265235
/** @var AddressInterface $address */
266236
$address = $this->addressRepositoryInterface->getById($addressId);
267-
} catch (NoSuchEntityException $exception) {
268-
throw new GraphQlNoSuchEntityException(
237+
} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) {
238+
throw new \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException(
269239
__('Address id %1 does not exist.', [$addressId])
270240
);
271241
}
272242
if ($address->getCustomerId() != $customer->getId()) {
273-
throw new GraphQlAuthorizationException(
243+
throw new \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException(
274244
__('Current customer does not have permission to delete address id %1', [$addressId])
275245
);
276246
}
277247
if ($address->isDefaultBilling()) {
278-
throw new GraphQlAuthorizationException(
248+
throw new \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException(
279249
__('Customer Address %1 is set as default billing address and can not be deleted', [$addressId])
280250
);
281251
}
282252
if ($address->isDefaultShipping()) {
283-
throw new GraphQlAuthorizationException(
253+
throw new \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException(
284254
__('Customer Address %1 is set as default shipping address and can not be deleted', [$addressId])
285255
);
286256
}

0 commit comments

Comments
 (0)