8
8
namespace Magento \CustomerGraphQl \Model \Resolver ;
9
9
10
10
use Magento \Authorization \Model \UserContextInterface ;
11
- use Magento \Customer \Api \Data \CustomerInterface ;
12
11
use Magento \Customer \Api \CustomerRepositoryInterface ;
13
12
use Magento \Customer \Api \AddressRepositoryInterface ;
13
+ use Magento \Customer \Api \AddressMetadataManagementInterface ;
14
14
use Magento \Customer \Api \Data \AddressInterfaceFactory ;
15
15
use Magento \Customer \Api \Data \AddressInterface ;
16
16
use Magento \Framework \Api \DataObjectHelper ;
19
19
use Magento \Framework \GraphQl \Query \ResolverInterface ;
20
20
use Magento \CustomerGraphQl \Model \Resolver \Address \AddressDataProvider ;
21
21
use Magento \Eav \Model \Config ;
22
+ use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
23
+ use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
24
+ use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
25
+ use Magento \Framework \Exception \NoSuchEntityException ;
22
26
23
27
/**
24
28
* Customers Address, used for GraphQL request processing.
25
29
*/
26
30
class Address implements ResolverInterface
27
31
{
28
- /**
29
- * Input data key
30
- */
31
- const CUSTOM_ATTRIBUTE_KEY = 'custom_attributes ' ;
32
- const EXTENSION_ATTRIBUTE_KEY = 'extension_attributes ' ;
33
-
34
32
/**
35
33
* Mutation Address type
36
34
*/
37
35
const MUTATION_ADDRESS_CREATE = 'customerAddressCreate ' ;
38
36
const MUTATION_ADDRESS_UPDATE = 'customerAddressUpdate ' ;
39
37
const MUTATION_ADDRESS_DELETE = 'customerAddressDelete ' ;
40
38
41
- /**
42
- * @var CustomerRepositoryInterface
43
- */
44
- private $ customerRepositoryInterface ;
45
-
46
39
/**
47
40
* @var AddressRepositoryInterface
48
41
*/
@@ -64,7 +57,7 @@ class Address implements ResolverInterface
64
57
private $ addressDataProvider ;
65
58
66
59
/**
67
- * @var \Magento\Framework\Api\ DataObjectHelper
60
+ * @var DataObjectHelper
68
61
*/
69
62
private $ dataObjectHelper ;
70
63
@@ -74,29 +67,26 @@ class Address implements ResolverInterface
74
67
private $ addressAttributes ;
75
68
76
69
/**
77
- * @param CustomerRepositoryInterface $customerRepositoryInterface
78
70
* @param AddressRepositoryInterface $addressRepositoryInterface
79
71
* @param AddressInterfaceFactory $addressInterfaceFactory
80
72
* @param Config $eavConfig
81
73
* @param AddressDataProvider $addressDataProvider
82
74
* @param DataObjectHelper $dataObjectHelper
83
75
*/
84
76
public function __construct (
85
- CustomerRepositoryInterface $ customerRepositoryInterface ,
86
77
AddressRepositoryInterface $ addressRepositoryInterface ,
87
78
AddressInterfaceFactory $ addressInterfaceFactory ,
88
79
Config $ eavConfig ,
89
80
AddressDataProvider $ addressDataProvider ,
90
81
DataObjectHelper $ dataObjectHelper
91
82
) {
92
- $ this ->customerRepositoryInterface = $ customerRepositoryInterface ;
93
83
$ this ->addressRepositoryInterface = $ addressRepositoryInterface ;
94
84
$ this ->addressInterfaceFactory = $ addressInterfaceFactory ;
95
85
$ this ->eavConfig = $ eavConfig ;
96
86
$ this ->addressDataProvider = $ addressDataProvider ;
97
87
$ this ->dataObjectHelper = $ dataObjectHelper ;
98
88
$ this ->addressAttributes = $ this ->eavConfig ->getEntityAttributes (
99
- \ Magento \ Customer \ Api \ AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS
89
+ AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS
100
90
);
101
91
}
102
92
@@ -112,40 +102,58 @@ public function resolve(
112
102
) {
113
103
/** @var \Magento\Framework\GraphQl\Query\Resolver\ContextInterface $context */
114
104
if ((!$ context ->getUserId ()) || $ context ->getUserType () == UserContextInterface::USER_TYPE_GUEST ) {
115
- throw new \ Magento \ Framework \ GraphQl \ Exception \ GraphQlAuthorizationException (
105
+ throw new GraphQlAuthorizationException (
116
106
__ (
117
107
'Current customer does not have access to the resource "%1" ' ,
118
- [\ Magento \ Customer \ Model \Customer:: ENTITY ]
108
+ [AddressMetadataManagementInterface:: ENTITY_TYPE_ADDRESS ]
119
109
)
120
110
);
121
111
}
122
- /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
123
- $ customer = $ this ->customerRepositoryInterface ->getById ($ context ->getUserId ());
112
+ $ customerId = $ context ->getUserId ();
124
113
switch ($ field ->getName ()) {
125
114
case self ::MUTATION_ADDRESS_CREATE :
126
115
return $ this ->addressDataProvider ->processCustomerAddress (
127
- $ this ->processCustomerAddressCreate ($ customer , $ args ['input ' ])
116
+ $ this ->processCustomerAddressCreate ($ customerId , $ args ['input ' ])
128
117
);
129
118
case self ::MUTATION_ADDRESS_UPDATE :
130
119
return $ this ->addressDataProvider ->processCustomerAddress (
131
- $ this ->processCustomerAddressUpdate ($ customer , $ args ['id ' ], $ args ['input ' ])
120
+ $ this ->processCustomerAddressUpdate ($ customerId , $ args ['id ' ], $ args ['input ' ])
132
121
);
133
122
case self ::MUTATION_ADDRESS_DELETE :
134
- return $ this ->processCustomerAddressDelete ($ customer , $ args ['id ' ]);
123
+ return $ this ->processCustomerAddressDelete ($ customerId , $ args ['id ' ]);
135
124
default :
136
125
return [];
137
126
}
138
127
}
139
128
140
129
/**
141
- * Get input address attribute errors
130
+ * Get new address attribute input errors
131
+ *
132
+ * @param array $addressInput
133
+ * @return bool|string
134
+ */
135
+ private function getNewAddressInputError (array $ addressInput )
136
+ {
137
+ foreach ($ this ->addressAttributes as $ attributeName => $ attributeInfo ) {
138
+ if ($ attributeInfo ->getIsRequired ()
139
+ && (!isset ($ addressInput [$ attributeName ]) || empty ($ addressInput [$ attributeName ]))) {
140
+ return $ attributeName ;
141
+ }
142
+ }
143
+ return false ;
144
+ }
145
+
146
+ /**
147
+ * Get update address attribute input errors
148
+ *
142
149
* @param array $addressInput
143
150
* @return bool|string
144
151
*/
145
- private function getAddressInputError (array $ addressInput )
152
+ private function getUpdateAddressInputError (array $ addressInput )
146
153
{
147
154
foreach ($ this ->addressAttributes as $ attributeName => $ attributeInfo ) {
148
- if ($ attributeInfo ->getIsRequired () && !isset ($ addressInput [$ attributeName ])) {
155
+ if ($ attributeInfo ->getIsRequired ()
156
+ && (isset ($ addressInput [$ attributeName ]) && empty ($ addressInput [$ attributeName ]))) {
149
157
return $ attributeName ;
150
158
}
151
159
}
@@ -154,6 +162,7 @@ private function getAddressInputError(array $addressInput)
154
162
155
163
/**
156
164
* Add $addressInput array information to a $address object
165
+ *
157
166
* @param AddressInterface $address
158
167
* @param array $addressInput
159
168
* @return AddressInterface
@@ -170,16 +179,17 @@ private function fillAddress(AddressInterface $address, array $addressInput) : A
170
179
171
180
/**
172
181
* Process customer address create
173
- * @param CustomerInterface $customer
182
+ *
183
+ * @param int $customerId
174
184
* @param array $addressInput
175
185
* @return AddressInterface
176
- * @throws \Magento\Framework\GraphQl\Exception\ GraphQlInputException
186
+ * @throws GraphQlInputException
177
187
*/
178
- private function processCustomerAddressCreate (CustomerInterface $ customer , array $ addressInput ) : AddressInterface
188
+ private function processCustomerAddressCreate ($ customerId , array $ addressInput ) : AddressInterface
179
189
{
180
- $ errorInput = $ this ->getAddressInputError ($ addressInput );
190
+ $ errorInput = $ this ->getNewAddressInputError ($ addressInput );
181
191
if ($ errorInput ) {
182
- throw new \ Magento \ Framework \ GraphQl \ Exception \ GraphQlInputException (
192
+ throw new GraphQlInputException (
183
193
__ ('Required parameter %1 is missing ' , [$ errorInput ])
184
194
);
185
195
}
@@ -188,69 +198,78 @@ private function processCustomerAddressCreate(CustomerInterface $customer, array
188
198
$ this ->addressInterfaceFactory ->create (),
189
199
$ addressInput
190
200
);
191
- $ newAddress ->setCustomerId ($ customer -> getId () );
201
+ $ newAddress ->setCustomerId ($ customerId );
192
202
return $ this ->addressRepositoryInterface ->save ($ newAddress );
193
203
}
194
204
195
205
/**
196
206
* Process customer address update
197
- * @param CustomerInterface $customer
207
+ *
208
+ * @param int $customerId
198
209
* @param int $addressId
199
210
* @param array $addressInput
200
211
* @return AddressInterface
201
- * @throws \Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException
202
- * @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException
212
+ * @throws GraphQlAuthorizationException
213
+ * @throws GraphQlNoSuchEntityException
214
+ * @throws GraphQlInputException
203
215
*/
204
- private function processCustomerAddressUpdate (CustomerInterface $ customer , $ addressId , array $ addressInput )
216
+ private function processCustomerAddressUpdate ($ customerId , $ addressId , array $ addressInput )
205
217
{
206
218
try {
207
219
/** @var AddressInterface $address */
208
220
$ address = $ this ->addressRepositoryInterface ->getById ($ addressId );
209
- } catch (\ Magento \ Framework \ Exception \ NoSuchEntityException $ exception ) {
210
- throw new \ Magento \ Framework \ GraphQl \ Exception \ GraphQlNoSuchEntityException (
221
+ } catch (NoSuchEntityException $ exception ) {
222
+ throw new GraphQlNoSuchEntityException (
211
223
__ ('Address id %1 does not exist. ' , [$ addressId ])
212
224
);
213
225
}
214
- if ($ address ->getCustomerId () != $ customer -> getId () ) {
215
- throw new \ Magento \ Framework \ GraphQl \ Exception \ GraphQlAuthorizationException (
226
+ if ($ address ->getCustomerId () != $ customerId ) {
227
+ throw new GraphQlAuthorizationException (
216
228
__ ('Current customer does not have permission to update address id %1 ' , [$ addressId ])
217
229
);
218
230
}
231
+ $ errorInput = $ this ->getUpdateAddressInputError ($ addressInput );
232
+ if ($ errorInput ) {
233
+ throw new GraphQlInputException (
234
+ __ ('Required parameter %1 is missing ' , [$ errorInput ])
235
+ );
236
+ }
219
237
return $ this ->addressRepositoryInterface ->save (
220
238
$ this ->fillAddress ($ address , $ addressInput )
221
239
);
222
240
}
223
241
224
242
/**
225
243
* Process customer address delete
226
- * @param CustomerInterface $customer
244
+ *
245
+ * @param int $customerId
227
246
* @param int $addressId
228
247
* @return bool
229
- * @throws \Magento\Framework\GraphQl\Exception\ GraphQlAuthorizationException
230
- * @throws \Magento\Framework\GraphQl\Exception\ GraphQlNoSuchEntityException
248
+ * @throws GraphQlAuthorizationException
249
+ * @throws GraphQlNoSuchEntityException
231
250
*/
232
- private function processCustomerAddressDelete (CustomerInterface $ customer , $ addressId )
251
+ private function processCustomerAddressDelete ($ customerId , $ addressId )
233
252
{
234
253
try {
235
254
/** @var AddressInterface $address */
236
255
$ address = $ this ->addressRepositoryInterface ->getById ($ addressId );
237
- } catch (\ Magento \ Framework \ Exception \ NoSuchEntityException $ exception ) {
238
- throw new \ Magento \ Framework \ GraphQl \ Exception \ GraphQlNoSuchEntityException (
256
+ } catch (NoSuchEntityException $ exception ) {
257
+ throw new GraphQlNoSuchEntityException (
239
258
__ ('Address id %1 does not exist. ' , [$ addressId ])
240
259
);
241
260
}
242
- if ($ address -> getCustomerId () != $ customer -> getId ()) {
243
- throw new \ Magento \ Framework \ GraphQl \ Exception \ GraphQlAuthorizationException (
261
+ if ($ customerId != $ address -> getCustomerId ()) {
262
+ throw new GraphQlAuthorizationException (
244
263
__ ('Current customer does not have permission to delete address id %1 ' , [$ addressId ])
245
264
);
246
265
}
247
266
if ($ address ->isDefaultBilling ()) {
248
- throw new \ Magento \ Framework \ GraphQl \ Exception \ GraphQlAuthorizationException (
267
+ throw new GraphQlAuthorizationException (
249
268
__ ('Customer Address %1 is set as default billing address and can not be deleted ' , [$ addressId ])
250
269
);
251
270
}
252
271
if ($ address ->isDefaultShipping ()) {
253
- throw new \ Magento \ Framework \ GraphQl \ Exception \ GraphQlAuthorizationException (
272
+ throw new GraphQlAuthorizationException (
254
273
__ ('Customer Address %1 is set as default shipping address and can not be deleted ' , [$ addressId ])
255
274
);
256
275
}
0 commit comments