14
14
15
15
/**
16
16
* Customer repository.
17
+ *
17
18
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18
19
*/
19
20
class CustomerRepository implements \Magento \Customer \Api \CustomerRepositoryInterface
@@ -131,15 +132,19 @@ public function __construct(
131
132
132
133
/**
133
134
* {@inheritdoc}
134
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
135
- * @SuppressWarnings(PHPMD.NPathComplexity)
136
135
*/
137
136
public function save (\Magento \Customer \Api \Data \CustomerInterface $ customer , $ passwordHash = null )
138
137
{
139
138
$ prevCustomerData = null ;
139
+ $ prevCustomerDataArr = null ;
140
+
140
141
if ($ customer ->getId ()) {
141
142
$ prevCustomerData = $ this ->getById ($ customer ->getId ());
143
+ $ prevCustomerDataArr = $ prevCustomerData ->__toArray ();
142
144
}
145
+
146
+ /** @var $customer \Magento\Customer\Model\Data\Customer */
147
+ $ customerArr = $ customer ->__toArray ();
143
148
$ customer = $ this ->imageProcessor ->save (
144
149
$ customer ,
145
150
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER ,
@@ -151,15 +156,17 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
151
156
$ customerData = $ this ->extensibleDataObjectConverter ->toNestedArray (
152
157
$ customer ,
153
158
[],
154
- ' \Magento\Customer\Api\Data\CustomerInterface '
159
+ \Magento \Customer \Api \Data \CustomerInterface::class
155
160
);
156
161
157
162
$ customer ->setAddresses ($ origAddresses );
158
163
$ customerModel = $ this ->customerFactory ->create (['data ' => $ customerData ]);
159
164
$ storeId = $ customerModel ->getStoreId ();
165
+
160
166
if ($ storeId === null ) {
161
167
$ customerModel ->setStoreId ($ this ->storeManager ->getStore ()->getId ());
162
168
}
169
+
163
170
$ customerModel ->setId ($ customer ->getId ());
164
171
165
172
// Need to use attribute set or future updates can cause data loss
@@ -169,19 +176,7 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
169
176
);
170
177
}
171
178
// Populate model with secure data
172
- if ($ customer ->getId ()) {
173
- $ customerSecure = $ this ->customerRegistry ->retrieveSecureData ($ customer ->getId ());
174
- $ customerModel ->setRpToken ($ customerSecure ->getRpToken ());
175
- $ customerModel ->setRpTokenCreatedAt ($ customerSecure ->getRpTokenCreatedAt ());
176
- $ customerModel ->setPasswordHash ($ customerSecure ->getPasswordHash ());
177
- $ customerModel ->setFailuresNum ($ customerSecure ->getFailuresNum ());
178
- $ customerModel ->setFirstFailure ($ customerSecure ->getFirstFailure ());
179
- $ customerModel ->setLockExpires ($ customerSecure ->getLockExpires ());
180
- } else {
181
- if ($ passwordHash ) {
182
- $ customerModel ->setPasswordHash ($ passwordHash );
183
- }
184
- }
179
+ $ this ->populateCustomerModelWithSecureData ($ customer , $ passwordHash , $ customerModel );
185
180
186
181
// If customer email was changed, reset RpToken info
187
182
if ($ prevCustomerData
@@ -190,36 +185,16 @@ public function save(\Magento\Customer\Api\Data\CustomerInterface $customer, $pa
190
185
$ customerModel ->setRpToken (null );
191
186
$ customerModel ->setRpTokenCreatedAt (null );
192
187
}
188
+
189
+ $ this ->setDefaultBilling ($ customerArr , $ prevCustomerDataArr , $ customerModel );
190
+
191
+ $ this ->setDefaultShipping ($ customerArr , $ prevCustomerDataArr , $ customerModel );
192
+
193
193
$ customerModel ->save ();
194
194
$ this ->customerRegistry ->push ($ customerModel );
195
195
$ customerId = $ customerModel ->getId ();
196
196
197
- if ($ customer ->getAddresses () !== null ) {
198
- if ($ customer ->getId ()) {
199
- $ existingAddresses = $ this ->getById ($ customer ->getId ())->getAddresses ();
200
- $ getIdFunc = function ($ address ) {
201
- return $ address ->getId ();
202
- };
203
- $ existingAddressIds = array_map ($ getIdFunc , $ existingAddresses );
204
- } else {
205
- $ existingAddressIds = [];
206
- }
207
-
208
- $ savedAddressIds = [];
209
- foreach ($ customer ->getAddresses () as $ address ) {
210
- $ address ->setCustomerId ($ customerId )
211
- ->setRegion ($ address ->getRegion ());
212
- $ this ->addressRepository ->save ($ address );
213
- if ($ address ->getId ()) {
214
- $ savedAddressIds [] = $ address ->getId ();
215
- }
216
- }
217
-
218
- $ addressIdsToDelete = array_diff ($ existingAddressIds , $ savedAddressIds );
219
- foreach ($ addressIdsToDelete as $ addressId ) {
220
- $ this ->addressRepository ->deleteById ($ addressId );
221
- }
222
- }
197
+ $ this ->updateAddresses ($ customer , $ customerId );
223
198
224
199
$ savedCustomer = $ this ->get ($ customer ->getEmail (), $ customer ->getWebsiteId ());
225
200
$ this ->eventManager ->dispatch (
@@ -256,7 +231,10 @@ public function getList(SearchCriteriaInterface $searchCriteria)
256
231
$ searchResults ->setSearchCriteria ($ searchCriteria );
257
232
/** @var \Magento\Customer\Model\ResourceModel\Customer\Collection $collection */
258
233
$ collection = $ this ->customerFactory ->create ()->getCollection ();
259
- $ this ->extensionAttributesJoinProcessor ->process ($ collection , 'Magento\Customer\Api\Data\CustomerInterface ' );
234
+ $ this ->extensionAttributesJoinProcessor ->process (
235
+ $ collection ,
236
+ \Magento \Customer \Api \Data \CustomerInterface::class
237
+ );
260
238
// This is needed to make sure all the attributes are properly loaded
261
239
foreach ($ this ->customerMetadata ->getAllAttributesMetadata () as $ metadata ) {
262
240
$ collection ->addAttributeToSelect ($ metadata ->getAttributeCode ());
@@ -336,4 +314,112 @@ protected function addFilterGroupToCollection(
336
314
$ collection ->addFieldToFilter ($ fields );
337
315
}
338
316
}
317
+
318
+ /**
319
+ * Update customer addresses.
320
+ *
321
+ * @param \Magento\Framework\Api\CustomAttributesDataInterface $customer
322
+ * @param int $customerId
323
+ * @return void
324
+ * @throws \Magento\Framework\Exception\InputException
325
+ */
326
+ private function updateAddresses (\Magento \Framework \Api \CustomAttributesDataInterface $ customer , int $ customerId )
327
+ {
328
+ if ($ customer ->getAddresses () !== null ) {
329
+ if ($ customer ->getId ()) {
330
+ $ existingAddresses = $ this ->getById ($ customer ->getId ())->getAddresses ();
331
+ $ getIdFunc = function ($ address ) {
332
+ return $ address ->getId ();
333
+ };
334
+ $ existingAddressIds = array_map ($ getIdFunc , $ existingAddresses );
335
+ } else {
336
+ $ existingAddressIds = [];
337
+ }
338
+
339
+ $ savedAddressIds = [];
340
+ foreach ($ customer ->getAddresses () as $ address ) {
341
+ $ address ->setCustomerId ($ customerId )
342
+ ->setRegion ($ address ->getRegion ());
343
+ $ this ->addressRepository ->save ($ address );
344
+ if ($ address ->getId ()) {
345
+ $ savedAddressIds [] = $ address ->getId ();
346
+ }
347
+ }
348
+
349
+ $ addressIdsToDelete = array_diff ($ existingAddressIds , $ savedAddressIds );
350
+ foreach ($ addressIdsToDelete as $ addressId ) {
351
+ $ this ->addressRepository ->deleteById ($ addressId );
352
+ }
353
+ }
354
+ }
355
+
356
+ /**
357
+ * Populate customer model with secure data.
358
+ *
359
+ * @param \Magento\Framework\Api\CustomAttributesDataInterface $customer
360
+ * @param string $passwordHash
361
+ * @param \Magento\Customer\Model\Customer\Interceptor $customerModel
362
+ * @return void
363
+ */
364
+ private function populateCustomerModelWithSecureData (
365
+ \Magento \Framework \Api \CustomAttributesDataInterface $ customer ,
366
+ $ passwordHash ,
367
+ $ customerModel
368
+ ) {
369
+ if ($ customer ->getId ()) {
370
+ $ customerSecure = $ this ->customerRegistry ->retrieveSecureData ($ customer ->getId ());
371
+ $ customerModel ->setRpToken ($ customerSecure ->getRpToken ());
372
+ $ customerModel ->setRpTokenCreatedAt ($ customerSecure ->getRpTokenCreatedAt ());
373
+ $ customerModel ->setPasswordHash ($ customerSecure ->getPasswordHash ());
374
+ $ customerModel ->setFailuresNum ($ customerSecure ->getFailuresNum ());
375
+ $ customerModel ->setFirstFailure ($ customerSecure ->getFirstFailure ());
376
+ $ customerModel ->setLockExpires ($ customerSecure ->getLockExpires ());
377
+ } else {
378
+ if ($ passwordHash ) {
379
+ $ customerModel ->setPasswordHash ($ passwordHash );
380
+ }
381
+ }
382
+ }
383
+
384
+ /**
385
+ * Set default billing.
386
+ *
387
+ * @param array $customerArr
388
+ * @param array $prevCustomerDataArr
389
+ * @param \Magento\Customer\Model\Customer\Interceptor $customerModel
390
+ * @return void
391
+ */
392
+ private function setDefaultBilling (
393
+ $ customerArr ,
394
+ $ prevCustomerDataArr ,
395
+ $ customerModel
396
+ ) {
397
+ if (!array_key_exists ('default_billing ' , $ customerArr ) &&
398
+ null !== $ prevCustomerDataArr &&
399
+ array_key_exists ('default_billing ' , $ prevCustomerDataArr )
400
+ ) {
401
+ $ customerModel ->setDefaultBilling ($ prevCustomerDataArr ['default_billing ' ]);
402
+ }
403
+ }
404
+
405
+ /**
406
+ * Set default shipping.
407
+ *
408
+ * @param array $customerArr
409
+ * @param array $prevCustomerDataArr
410
+ * @param \Magento\Customer\Model\Customer\Interceptor $customerModel
411
+ * @return void
412
+ */
413
+ private function setDefaultShipping (
414
+ $ customerArr ,
415
+ $ prevCustomerDataArr ,
416
+ $ customerModel
417
+ ) {
418
+ if (!array_key_exists ('default_shipping ' , $ customerArr ) &&
419
+ null !== $ prevCustomerDataArr &&
420
+ array_key_exists ('default_shipping ' , $ prevCustomerDataArr )
421
+ ) {
422
+ $ customerModel ->setDefaultShipping ($ prevCustomerDataArr ['default_shipping ' ]);
423
+ }
424
+ }
339
425
}
0 commit comments