7
7
namespace Magento \Customer \Model \ResourceModel ;
8
8
9
9
use Magento \Customer \Api \CustomerMetadataInterface ;
10
+ use Magento \Customer \Api \Data \CustomerInterface ;
10
11
use Magento \Customer \Model \Delegation \Data \NewOperation ;
11
12
use Magento \Customer \Model \Customer \NotificationStorage ;
12
13
use Magento \Framework \Api \DataObjectHelper ;
@@ -163,13 +164,11 @@ public function __construct(
163
164
* {@inheritdoc}
164
165
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
165
166
* @SuppressWarnings(PHPMD.NPathComplexity)
166
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
167
167
*/
168
- public function save (\ Magento \ Customer \ Api \ Data \ CustomerInterface $ customer , $ passwordHash = null )
168
+ public function save (CustomerInterface $ customer , $ passwordHash = null )
169
169
{
170
170
/** @var NewOperation|null $delegatedNewOperation */
171
- $ delegatedNewOperation = !$ customer ->getId ()
172
- ? $ this ->delegatedStorage ->consumeNewOperation () : null ;
171
+ $ delegatedNewOperation = !$ customer ->getId () ? $ this ->delegatedStorage ->consumeNewOperation () : null ;
173
172
$ prevCustomerData = null ;
174
173
$ prevCustomerDataArr = null ;
175
174
if ($ customer ->getId ()) {
@@ -183,72 +182,48 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
183
182
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER ,
184
183
$ prevCustomerData
185
184
);
186
-
187
185
$ origAddresses = $ customer ->getAddresses ();
188
186
$ customer ->setAddresses ([]);
189
- $ customerData = $ this ->extensibleDataObjectConverter ->toNestedArray (
190
- $ customer ,
191
- [],
192
- \Magento \Customer \Api \Data \CustomerInterface::class
193
- );
194
-
187
+ $ customerData = $ this ->extensibleDataObjectConverter ->toNestedArray ($ customer , [], CustomerInterface::class);
195
188
$ customer ->setAddresses ($ origAddresses );
196
189
/** @var Customer $customerModel */
197
- $ customerModel = $ this ->customerFactory ->create (
198
- ['data ' => $ customerData ]
199
- );
200
- //Model's actual ID field maybe different than "id"
201
- //so "id" field from $customerData may be ignored.
190
+ $ customerModel = $ this ->customerFactory ->create (['data ' => $ customerData ]);
191
+ //Model's actual ID field maybe different than "id" so "id" field from $customerData may be ignored.
202
192
$ customerModel ->setId ($ customer ->getId ());
203
-
204
193
$ storeId = $ customerModel ->getStoreId ();
205
194
if ($ storeId === null ) {
206
195
$ customerModel ->setStoreId ($ this ->storeManager ->getStore ()->getId ());
207
196
}
208
-
209
197
// Need to use attribute set or future updates can cause data loss
210
198
if (!$ customerModel ->getAttributeSetId ()) {
211
- $ customerModel ->setAttributeSetId (
212
- \Magento \Customer \Api \CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER
213
- );
199
+ $ customerModel ->setAttributeSetId (CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER );
214
200
}
215
201
$ this ->populateCustomerWithSecureData ($ customerModel , $ passwordHash );
216
-
217
202
// If customer email was changed, reset RpToken info
218
- if ($ prevCustomerData
219
- && $ prevCustomerData ->getEmail () !== $ customerModel ->getEmail ()
220
- ) {
203
+ if ($ prevCustomerData && $ prevCustomerData ->getEmail () !== $ customerModel ->getEmail ()) {
221
204
$ customerModel ->setRpToken (null );
222
205
$ customerModel ->setRpTokenCreatedAt (null );
223
206
}
224
207
if (!array_key_exists ('default_billing ' , $ customerArr )
225
208
&& null !== $ prevCustomerDataArr
226
209
&& array_key_exists ('default_billing ' , $ prevCustomerDataArr )
227
210
) {
228
- $ customerModel ->setDefaultBilling (
229
- $ prevCustomerDataArr ['default_billing ' ]
230
- );
211
+ $ customerModel ->setDefaultBilling ($ prevCustomerDataArr ['default_billing ' ]);
231
212
}
232
213
if (!array_key_exists ('default_shipping ' , $ customerArr )
233
214
&& null !== $ prevCustomerDataArr
234
215
&& array_key_exists ('default_shipping ' , $ prevCustomerDataArr )
235
216
) {
236
- $ customerModel ->setDefaultShipping (
237
- $ prevCustomerDataArr ['default_shipping ' ]
238
- );
217
+ $ customerModel ->setDefaultShipping ($ prevCustomerDataArr ['default_shipping ' ]);
239
218
}
240
-
241
219
$ customerModel ->save ();
242
220
$ this ->customerRegistry ->push ($ customerModel );
243
221
$ customerId = $ customerModel ->getId ();
244
-
245
222
if (!$ customer ->getAddresses ()
246
223
&& $ delegatedNewOperation
247
224
&& $ delegatedNewOperation ->getCustomer ()->getAddresses ()
248
225
) {
249
- $ customer ->setAddresses (
250
- $ delegatedNewOperation ->getCustomer ()->getAddresses ()
251
- );
226
+ $ customer ->setAddresses ($ delegatedNewOperation ->getCustomer ()->getAddresses ());
252
227
}
253
228
if ($ customer ->getAddresses () !== null ) {
254
229
if ($ customer ->getId ()) {
@@ -260,7 +235,6 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
260
235
} else {
261
236
$ existingAddressIds = [];
262
237
}
263
-
264
238
$ savedAddressIds = [];
265
239
foreach ($ customer ->getAddresses () as $ address ) {
266
240
$ address ->setCustomerId ($ customerId )
@@ -270,22 +244,19 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
270
244
$ savedAddressIds [] = $ address ->getId ();
271
245
}
272
246
}
273
-
274
247
$ addressIdsToDelete = array_diff ($ existingAddressIds , $ savedAddressIds );
275
248
foreach ($ addressIdsToDelete as $ addressId ) {
276
249
$ this ->addressRepository ->deleteById ($ addressId );
277
250
}
278
251
}
279
252
$ this ->customerRegistry ->remove ($ customerId );
280
253
$ savedCustomer = $ this ->get ($ customer ->getEmail (), $ customer ->getWebsiteId ());
281
-
282
254
$ this ->eventManager ->dispatch (
283
255
'customer_save_after_data_object ' ,
284
256
[
285
257
'customer_data_object ' => $ savedCustomer ,
286
258
'orig_customer_data_object ' => $ prevCustomerData ,
287
- 'delegate_data ' => $ delegatedNewOperation
288
- ? $ delegatedNewOperation ->getAdditionalData () : []
259
+ 'delegate_data ' => $ delegatedNewOperation ? $ delegatedNewOperation ->getAdditionalData () : [],
289
260
]
290
261
);
291
262
@@ -350,7 +321,7 @@ public function getList(SearchCriteriaInterface $searchCriteria)
350
321
$ collection = $ this ->customerFactory ->create ()->getCollection ();
351
322
$ this ->extensionAttributesJoinProcessor ->process (
352
323
$ collection ,
353
- \ Magento \ Customer \ Api \ Data \ CustomerInterface::class
324
+ CustomerInterface::class
354
325
);
355
326
// This is needed to make sure all the attributes are properly loaded
356
327
foreach ($ this ->customerMetadata ->getAllAttributesMetadata () as $ metadata ) {
@@ -382,7 +353,7 @@ public function getList(SearchCriteriaInterface $searchCriteria)
382
353
/**
383
354
* {@inheritdoc}
384
355
*/
385
- public function delete (\ Magento \ Customer \ Api \ Data \ CustomerInterface $ customer )
356
+ public function delete (CustomerInterface $ customer )
386
357
{
387
358
return $ this ->deleteById ($ customer ->getId ());
388
359
}
0 commit comments