12
12
use Magento \Eav \Model \Entity \Attribute \AbstractAttribute ;
13
13
use Magento \Store \Model \Store ;
14
14
use Magento \ImportExport \Model \Import ;
15
+ use Magento \CustomerImportExport \Model \ResourceModel \Import \Address \Storage as AddressStorage ;
15
16
16
17
/**
17
18
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -100,6 +101,8 @@ class Address extends AbstractCustomer
100
101
* )
101
102
*
102
103
* @var array
104
+ * @deprected
105
+ * @see $addressStorage
103
106
*/
104
107
protected $ _addresses = [];
105
108
@@ -256,6 +259,11 @@ class Address extends AbstractCustomer
256
259
*/
257
260
private $ optionsByWebsite = [];
258
261
262
+ /**
263
+ * @var AddressStorage
264
+ */
265
+ private $ addressStorage ;
266
+
259
267
/**
260
268
* @param \Magento\Framework\Stdlib\StringUtils $string
261
269
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
@@ -276,6 +284,7 @@ class Address extends AbstractCustomer
276
284
* @param \Magento\Customer\Model\Address\Validator\Postcode $postcodeValidator
277
285
* @param array $data
278
286
* @param Sources\CountryWithWebsites|null $countryWithWebsites
287
+ * @param AddressStorage|null $addressStorage
279
288
*
280
289
* @SuppressWarnings(PHPMD.NPathComplexity)
281
290
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -299,7 +308,8 @@ public function __construct(
299
308
DateTime $ dateTime ,
300
309
\Magento \Customer \Model \Address \Validator \Postcode $ postcodeValidator ,
301
310
array $ data = [],
302
- Sources \CountryWithWebsites $ countryWithWebsites = null
311
+ Sources \CountryWithWebsites $ countryWithWebsites = null ,
312
+ AddressStorage $ addressStorage = null
303
313
) {
304
314
$ this ->_customerFactory = $ customerFactory ;
305
315
$ this ->_addressFactory = $ addressFactory ;
@@ -352,6 +362,8 @@ public function __construct(
352
362
self ::ERROR_DUPLICATE_PK ,
353
363
__ ('We found another row with this email, website and address ID combination. ' )
354
364
);
365
+ $ this ->addressStorage = $ addressStorage
366
+ ?: ObjectManager::getInstance ()->get (AddressStorage::class);
355
367
356
368
$ this ->_initAttributes ();
357
369
$ this ->_initCountryRegions ();
@@ -482,10 +494,11 @@ protected function _initAddresses()
482
494
public function prepareCustomerData (\Traversable $ rows )
483
495
{
484
496
parent ::prepareCustomerData ($ rows );
497
+
485
498
$ ids = [];
486
499
foreach ($ rows as $ customerData ) {
487
500
if (isset ($ customerData [static ::COLUMN_EMAIL ])
488
- && $ email = $ customerData [static ::COLUMN_EMAIL ]
501
+ && ( $ email = $ customerData [static ::COLUMN_EMAIL ])
489
502
&& isset ($ customerData [static ::COLUMN_WEBSITE ])
490
503
&& (
491
504
$ websiteId = $ this ->getWebsiteId (
@@ -501,21 +514,7 @@ public function prepareCustomerData(\Traversable $rows)
501
514
}
502
515
}
503
516
504
- $ this ->_addressCollection ->addFieldToFilter (
505
- 'parent_id ' ,
506
- ['in ' => $ ids ]
507
- );
508
- /** @var $address \Magento\Customer\Model\Address */
509
- foreach ($ this ->_addressCollection as $ address ) {
510
- $ customerId = $ address ->getParentId ();
511
- if (!isset ($ this ->_addresses [$ customerId ])) {
512
- $ this ->_addresses [$ customerId ] = [];
513
- }
514
- $ addressId = $ address ->getId ();
515
- if (!in_array ($ addressId , $ this ->_addresses [$ customerId ])) {
516
- $ this ->_addresses [$ customerId ][] = $ addressId ;
517
- }
518
- }
517
+ $ this ->addressStorage ->prepareAddresses ($ ids );
519
518
}
520
519
521
520
/**
@@ -644,9 +643,10 @@ protected function _prepareDataForUpdate(array $rowData)
644
643
$ defaults = [];
645
644
$ newAddress = true ;
646
645
// get address id
647
- if (isset ($ this ->_addresses [$ customerId ])
648
- && in_array ($ rowData [self ::COLUMN_ADDRESS_ID ], $ this ->_addresses [$ customerId ])
649
- ) {
646
+ if ($ this ->addressStorage ->doesExist (
647
+ $ rowData [self ::COLUMN_ADDRESS_ID ],
648
+ $ customerId
649
+ )) {
650
650
$ newAddress = false ;
651
651
$ addressId = $ rowData [self ::COLUMN_ADDRESS_ID ];
652
652
} else {
@@ -901,12 +901,11 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
901
901
$ rowNumber ,
902
902
$ multiSeparator
903
903
);
904
- } elseif ($ attributeParams ['is_required ' ] && (!isset (
905
- $ this ->_addresses [$ customerId ]
906
- ) || !in_array (
907
- $ addressId ,
908
- $ this ->_addresses [$ customerId ]
909
- ))
904
+ } elseif ($ attributeParams ['is_required ' ]
905
+ && !$ this ->addressStorage ->doesExist (
906
+ $ addressId ,
907
+ $ customerId
908
+ )
910
909
) {
911
910
$ this ->addRowError (self ::ERROR_VALUE_IS_REQUIRED , $ rowNumber , $ attributeCode );
912
911
}
@@ -962,9 +961,10 @@ protected function _validateRowForDelete(array $rowData, $rowNumber)
962
961
} else {
963
962
if (!strlen ($ addressId )) {
964
963
$ this ->addRowError (self ::ERROR_ADDRESS_ID_IS_EMPTY , $ rowNumber );
965
- } elseif (!in_array ($ customerId , $ this ->_addresses )
966
- || !in_array ($ addressId , $ this ->_addresses [$ customerId ])
967
- ) {
964
+ } elseif (!$ this ->addressStorage ->doesExist (
965
+ $ addressId ,
966
+ $ customerId
967
+ )) {
968
968
$ this ->addRowError (self ::ERROR_ADDRESS_NOT_FOUND , $ rowNumber );
969
969
}
970
970
}
@@ -980,7 +980,7 @@ protected function _validateRowForDelete(array $rowData, $rowNumber)
980
980
*/
981
981
protected function _checkRowDuplicate ($ customerId , $ addressId )
982
982
{
983
- if (isset ( $ this ->_addresses [ $ customerId ]) && in_array ($ addressId , $ this -> _addresses [ $ customerId] )) {
983
+ if ($ this ->addressStorage -> doesExist ($ addressId , $ customerId )) {
984
984
if (!isset ($ this ->_importedRowPks [$ customerId ][$ addressId ])) {
985
985
$ this ->_importedRowPks [$ customerId ][$ addressId ] = true ;
986
986
return false ;
0 commit comments