9
9
10
10
use Magento \Authorization \Model \UserContextInterface ;
11
11
use Magento \Customer \Api \Data \CustomerInterface ;
12
- use Magento \Customer \Api \AddressMetadataManagementInterface ;
13
12
use Magento \Customer \Api \CustomerRepositoryInterface ;
14
13
use Magento \Customer \Api \AddressRepositoryInterface ;
15
14
use Magento \Customer \Api \Data \AddressInterfaceFactory ;
16
15
use Magento \Customer \Api \Data \AddressInterface ;
17
16
use Magento \Framework \Api \DataObjectHelper ;
18
17
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
19
18
use Magento \Framework \GraphQl \Config \Element \Field ;
20
- use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
21
19
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 ;
25
20
use Magento \CustomerGraphQl \Model \Resolver \Address \AddressDataProvider ;
26
21
use Magento \Eav \Model \Config ;
27
22
30
25
*/
31
26
class Address implements ResolverInterface
32
27
{
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
-
55
28
/**
56
29
* Input data key
57
30
*/
@@ -65,7 +38,6 @@ class Address implements ResolverInterface
65
38
const MUTATION_ADDRESS_UPDATE = 'customerAddressUpdate ' ;
66
39
const MUTATION_ADDRESS_DELETE = 'customerAddressDelete ' ;
67
40
68
-
69
41
/**
70
42
* @var CustomerRepositoryInterface
71
43
*/
@@ -97,6 +69,12 @@ class Address implements ResolverInterface
97
69
private $ dataObjectHelper ;
98
70
99
71
/**
72
+ * @var array
73
+ */
74
+ private $ addressAttributes ;
75
+
76
+ /**
77
+ * @param CustomerRepositoryInterface $customerRepositoryInterface
100
78
* @param AddressRepositoryInterface $addressRepositoryInterface
101
79
* @param AddressInterfaceFactory $addressInterfaceFactory
102
80
* @param Config $eavConfig
@@ -117,6 +95,9 @@ public function __construct(
117
95
$ this ->eavConfig = $ eavConfig ;
118
96
$ this ->addressDataProvider = $ addressDataProvider ;
119
97
$ this ->dataObjectHelper = $ dataObjectHelper ;
98
+ $ this ->addressAttributes = $ this ->eavConfig ->getEntityAttributes (
99
+ \Magento \Customer \Api \AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS
100
+ );
120
101
}
121
102
122
103
/**
@@ -131,7 +112,7 @@ public function resolve(
131
112
) {
132
113
/** @var \Magento\Framework\GraphQl\Query\Resolver\ContextInterface $context */
133
114
if ((!$ context ->getUserId ()) || $ context ->getUserType () == UserContextInterface::USER_TYPE_GUEST ) {
134
- throw new GraphQlAuthorizationException (
115
+ throw new \ Magento \ Framework \ GraphQl \ Exception \ GraphQlAuthorizationException (
135
116
__ (
136
117
'Current customer does not have access to the resource "%1" ' ,
137
118
[\Magento \Customer \Model \Customer::ENTITY ]
@@ -163,27 +144,14 @@ public function resolve(
163
144
*/
164
145
private function getAddressInputError (array $ addressInput )
165
146
{
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 ;
169
150
}
170
151
}
171
152
return false ;
172
153
}
173
154
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
-
187
155
/**
188
156
* Add $addressInput array information to a $address object
189
157
* @param AddressInterface $address
@@ -195,7 +163,7 @@ private function fillAddress(AddressInterface $address, array $addressInput) : A
195
163
$ this ->dataObjectHelper ->populateWithArray (
196
164
$ address ,
197
165
$ addressInput ,
198
- \ Magento \ Customer \ Api \ Data \ AddressInterface::class
166
+ AddressInterface::class
199
167
);
200
168
return $ address ;
201
169
}
@@ -205,13 +173,15 @@ private function fillAddress(AddressInterface $address, array $addressInput) : A
205
173
* @param CustomerInterface $customer
206
174
* @param array $addressInput
207
175
* @return AddressInterface
208
- * @throws GraphQlInputException
176
+ * @throws \Magento\Framework\GraphQl\Exception\ GraphQlInputException
209
177
*/
210
178
private function processCustomerAddressCreate (CustomerInterface $ customer , array $ addressInput ) : AddressInterface
211
179
{
212
180
$ errorInput = $ this ->getAddressInputError ($ addressInput );
213
181
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
+ );
215
185
}
216
186
/** @var AddressInterface $newAddress */
217
187
$ newAddress = $ this ->fillAddress (
@@ -228,21 +198,21 @@ private function processCustomerAddressCreate(CustomerInterface $customer, array
228
198
* @param int $addressId
229
199
* @param array $addressInput
230
200
* @return AddressInterface
231
- * @throws GraphQlAuthorizationException
232
- * @throws GraphQlNoSuchEntityException
201
+ * @throws \Magento\Framework\GraphQl\Exception\ GraphQlAuthorizationException
202
+ * @throws \Magento\Framework\GraphQl\Exception\ GraphQlNoSuchEntityException
233
203
*/
234
204
private function processCustomerAddressUpdate (CustomerInterface $ customer , $ addressId , array $ addressInput )
235
205
{
236
206
try {
237
207
/** @var AddressInterface $address */
238
208
$ 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 (
241
211
__ ('Address id %1 does not exist. ' , [$ addressId ])
242
212
);
243
213
}
244
214
if ($ address ->getCustomerId () != $ customer ->getId ()) {
245
- throw new GraphQlAuthorizationException (
215
+ throw new \ Magento \ Framework \ GraphQl \ Exception \ GraphQlAuthorizationException (
246
216
__ ('Current customer does not have permission to update address id %1 ' , [$ addressId ])
247
217
);
248
218
}
@@ -256,31 +226,31 @@ private function processCustomerAddressUpdate(CustomerInterface $customer, $addr
256
226
* @param CustomerInterface $customer
257
227
* @param int $addressId
258
228
* @return bool
259
- * @throws GraphQlAuthorizationException
260
- * @throws GraphQlNoSuchEntityException
229
+ * @throws \Magento\Framework\GraphQl\Exception\ GraphQlAuthorizationException
230
+ * @throws \Magento\Framework\GraphQl\Exception\ GraphQlNoSuchEntityException
261
231
*/
262
232
private function processCustomerAddressDelete (CustomerInterface $ customer , $ addressId )
263
233
{
264
234
try {
265
235
/** @var AddressInterface $address */
266
236
$ 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 (
269
239
__ ('Address id %1 does not exist. ' , [$ addressId ])
270
240
);
271
241
}
272
242
if ($ address ->getCustomerId () != $ customer ->getId ()) {
273
- throw new GraphQlAuthorizationException (
243
+ throw new \ Magento \ Framework \ GraphQl \ Exception \ GraphQlAuthorizationException (
274
244
__ ('Current customer does not have permission to delete address id %1 ' , [$ addressId ])
275
245
);
276
246
}
277
247
if ($ address ->isDefaultBilling ()) {
278
- throw new GraphQlAuthorizationException (
248
+ throw new \ Magento \ Framework \ GraphQl \ Exception \ GraphQlAuthorizationException (
279
249
__ ('Customer Address %1 is set as default billing address and can not be deleted ' , [$ addressId ])
280
250
);
281
251
}
282
252
if ($ address ->isDefaultShipping ()) {
283
- throw new GraphQlAuthorizationException (
253
+ throw new \ Magento \ Framework \ GraphQl \ Exception \ GraphQlAuthorizationException (
284
254
__ ('Customer Address %1 is set as default shipping address and can not be deleted ' , [$ addressId ])
285
255
);
286
256
}
0 commit comments