Skip to content

Commit 01a6ea4

Browse files
committed
Merge remote-tracking branch 'adobe-commerce-tier-4/ACP2E-3165' into Tier4-Kings-PR-08-23-2024
2 parents edcd0dc + 2bcbef1 commit 01a6ea4

File tree

5 files changed

+175
-83
lines changed

5 files changed

+175
-83
lines changed

app/code/Magento/CustomerImportExport/Model/Import/AbstractCustomer.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
namespace Magento\CustomerImportExport\Model\Import;
88

9-
use Magento\Customer\Model\Config\Share;
10-
use Magento\Framework\App\ObjectManager;
119
use Magento\Framework\Validator\EmailAddress;
1210
use Magento\Framework\Validator\ValidateException;
1311
use Magento\Framework\Validator\ValidatorChain;
@@ -89,11 +87,6 @@ abstract class AbstractCustomer extends \Magento\ImportExport\Model\Import\Entit
8987
*/
9088
protected $masterAttributeCode = '_email';
9189

92-
/**
93-
* @var Share
94-
*/
95-
private $configShare;
96-
9790
/**
9891
* @param \Magento\Framework\Stdlib\StringUtils $string
9992
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
@@ -106,7 +99,6 @@ abstract class AbstractCustomer extends \Magento\ImportExport\Model\Import\Entit
10699
* @param \Magento\Eav\Model\Config $eavConfig
107100
* @param \Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\StorageFactory $storageFactory
108101
* @param array $data
109-
* @param Share|null $configShare
110102
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
111103
*/
112104
public function __construct(
@@ -120,8 +112,7 @@ public function __construct(
120112
\Magento\ImportExport\Model\Export\Factory $collectionFactory,
121113
\Magento\Eav\Model\Config $eavConfig,
122114
\Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\StorageFactory $storageFactory,
123-
array $data = [],
124-
?Share $configShare = null
115+
array $data = []
125116
) {
126117
$this->_storageFactory = $storageFactory;
127118
parent::__construct(
@@ -136,7 +127,6 @@ public function __construct(
136127
$eavConfig,
137128
$data
138129
);
139-
$this->configShare = $configShare ?? ObjectManager::getInstance()->get(Share::class);
140130
$this->addMessageTemplate(self::ERROR_WEBSITE_IS_EMPTY, __('Please specify a website.'));
141131
$this->addMessageTemplate(
142132
self::ERROR_EMAIL_IS_EMPTY,
@@ -184,10 +174,6 @@ protected function _getCustomerId($email, $websiteCode)
184174
{
185175
$email = strtolower(trim($email));
186176

187-
if ($this->configShare->isGlobalScope()) {
188-
return $this->_customerStorage->getCustomerIdByEmail($email);
189-
}
190-
191177
if (isset($this->_websiteCodeToId[$websiteCode])) {
192178
$websiteId = $this->_websiteCodeToId[$websiteCode];
193179
return $this->_customerStorage->getCustomerId($email, $websiteId);

app/code/Magento/CustomerImportExport/Model/Import/Address.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace Magento\CustomerImportExport\Model\Import;
88

9-
use Magento\Customer\Model\Config\Share;
109
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites as CountryWithWebsitesSource;
1110
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
1211
use Magento\Framework\App\ObjectManager;
@@ -274,7 +273,6 @@ class Address extends AbstractCustomer
274273
* @param CountryWithWebsitesSource|null $countryWithWebsites
275274
* @param AddressStorage|null $addressStorage
276275
* @param Processor|null $indexerProcessor
277-
* @param Share|null $configShare
278276
*
279277
* @SuppressWarnings(PHPMD.NPathComplexity)
280278
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -299,8 +297,7 @@ public function __construct(
299297
array $data = [],
300298
?CountryWithWebsitesSource $countryWithWebsites = null,
301299
?AddressStorage $addressStorage = null,
302-
?Processor $indexerProcessor = null,
303-
?Share $configShare = null
300+
?Processor $indexerProcessor = null
304301
) {
305302
$this->_customerFactory = $customerFactory;
306303
$this->_addressFactory = $addressFactory;
@@ -328,8 +325,7 @@ public function __construct(
328325
$collectionFactory,
329326
$eavConfig,
330327
$storageFactory,
331-
$data,
332-
$configShare
328+
$data
333329
);
334330

335331
$this->_entityTable = isset(

app/code/Magento/CustomerImportExport/Model/ResourceModel/Import/Customer/Storage.php

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace Magento\CustomerImportExport\Model\ResourceModel\Import\Customer;
77

8-
use Magento\Customer\Api\CustomerRepositoryInterface;
8+
use Magento\Customer\Model\Config\Share;
99
use Magento\Customer\Model\ResourceModel\Customer\Collection as CustomerCollection;
1010
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
1111
use Magento\Framework\DataObject;
@@ -30,11 +30,6 @@ class Storage
3030
*/
3131
protected $_customerIds = [];
3232

33-
/**
34-
* @var array
35-
*/
36-
private $customerIdsByEmail = [];
37-
3833
/**
3934
* Number of items to fetch from db in one query
4035
*
@@ -67,26 +62,26 @@ class Storage
6762
private $customerStoreIds = [];
6863

6964
/**
70-
* @var CustomerRepositoryInterface
65+
* @var Share
7166
*/
72-
private $customerRepository;
67+
private $configShare;
7368

7469
/**
7570
* @param CustomerCollectionFactory $collectionFactory
76-
* @param CustomerRepositoryInterface $customerRepository
71+
* @param Share $configShare
7772
* @param array $data
7873
*/
7974
public function __construct(
8075
CustomerCollectionFactory $collectionFactory,
81-
CustomerRepositoryInterface $customerRepository,
76+
Share $configShare,
8277
array $data = []
8378
) {
8479
$this->_customerCollection = isset(
8580
$data['customer_collection']
8681
) ? $data['customer_collection'] : $collectionFactory->create();
8782
$this->_pageSize = isset($data['page_size']) ? (int) $data['page_size'] : 0;
8883
$this->customerCollectionFactory = $collectionFactory;
89-
$this->customerRepository = $customerRepository;
84+
$this->configShare = $configShare;
9085
}
9186

9287
/**
@@ -109,12 +104,23 @@ private function loadCustomersData(array $customerIdentifiers): void
109104
};
110105
$offset = 0;
111106
for ($chunk = $getChuck($offset); !empty($chunk); $offset += $pageSize, $chunk = $getChuck($offset)) {
112-
$emails = array_column($chunk, 'email');
107+
$customerWebsites = array_reduce($chunk, function ($customerWebsiteByEmail, $customer) {
108+
$customerWebsiteByEmail[$customer['email']][] = $customer['website_id'];
109+
return $customerWebsiteByEmail;
110+
}, []);
113111
$chunkSelect = clone $select;
114-
$chunkSelect->where($customerTableId . '.email IN (?)', $emails);
112+
$chunkSelect->where($customerTableId . '.email IN (?)', array_keys($customerWebsites));
115113
$customers = $collection->getConnection()->fetchAll($chunkSelect);
116114
foreach ($customers as $customer) {
117115
$this->addCustomerByArray($customer);
116+
if ($this->configShare->isGlobalScope() &&
117+
!in_array((int) $customer['website_id'], $customerWebsites[$customer['email']], true)
118+
) {
119+
foreach ($customerWebsites[$customer['email']] as $websiteId) {
120+
$customer['website_id'] = $websiteId;
121+
$this->addCustomerByArray($customer);
122+
}
123+
}
118124
}
119125
}
120126
}
@@ -179,25 +185,6 @@ public function getCustomerId(string $email, int $websiteId)
179185
return false;
180186
}
181187

182-
/**
183-
* Find customer ID by email.
184-
*
185-
* @param string $email
186-
* @return bool|int
187-
*/
188-
public function getCustomerIdByEmail(string $email)
189-
{
190-
if (!isset($this->customerIdsByEmail[$email])) {
191-
try {
192-
$this->customerIdsByEmail[$email] = $this->customerRepository->get($email)->getId();
193-
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
194-
$this->customerIdsByEmail[$email] = false;
195-
}
196-
}
197-
198-
return $this->customerIdsByEmail[$email];
199-
}
200-
201188
/**
202189
* Get previously loaded customer id.
203190
*

app/code/Magento/CustomerImportExport/Test/Unit/Model/Import/AddressTest.php

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Magento\Customer\Model\Address\Validator\Postcode;
1111
use Magento\Customer\Model\AddressFactory;
12-
use Magento\Customer\Model\Config\Share;
1312
use Magento\Customer\Model\CustomerFactory;
1413
use Magento\Customer\Model\Indexer\Processor;
1514
use Magento\Customer\Model\ResourceModel\Address\Attribute as AddressAttribute;
@@ -150,16 +149,6 @@ class AddressTest extends TestCase
150149
*/
151150
private $countryWithWebsites;
152151

153-
/**
154-
* @var Share|MockObject
155-
*/
156-
private $configShare;
157-
158-
/**
159-
* @var Storage
160-
*/
161-
private $customerStorage;
162-
163152
/**
164153
* Init entity adapter model
165154
*/
@@ -182,7 +171,6 @@ protected function setUp(): void
182171

183172
->method('getAllOptions')
184173
->willReturn([]);
185-
$this->configShare = $this->createMock(Share::class);
186174
$this->_model = $this->_getModelMock();
187175
$this->errorAggregator = $this->createPartialMock(
188176
ProcessingErrorAggregator::class,
@@ -210,7 +198,7 @@ protected function _getModelDependencies()
210198
->getMock();
211199
$connection = $this->createMock(\stdClass::class);
212200
$attributeCollection = $this->_createAttrCollectionMock();
213-
$this->customerStorage = $this->_createCustomerStorageMock();
201+
$customerStorage = $this->_createCustomerStorageMock();
214202
$customerEntity = $this->_createCustomerEntityMock();
215203
$addressCollection = new Collection(
216204
$this->createMock(EntityFactory::class)
@@ -234,7 +222,7 @@ protected function _getModelDependencies()
234222
'bunch_size' => 1,
235223
'attribute_collection' => $attributeCollection,
236224
'entity_type_id' => 1,
237-
'customer_storage' => $this->customerStorage,
225+
'customer_storage' => $customerStorage,
238226
'customer_entity' => $customerEntity,
239227
'address_collection' => $addressCollection,
240228
'entity_table' => 'not_used',
@@ -400,8 +388,7 @@ protected function _getModelMock()
400388
$this->_getModelDependencies(),
401389
$this->countryWithWebsites,
402390
$this->createMock(\Magento\CustomerImportExport\Model\ResourceModel\Import\Address\Storage::class),
403-
$this->createMock(Processor::class),
404-
$this->configShare
391+
$this->createMock(Processor::class)
405392
);
406393

407394
$property = new \ReflectionProperty($modelMock, '_availableBehaviors');
@@ -460,10 +447,6 @@ public function testValidateRowForUpdate(array $rowData, array $errors, $isValid
460447
{
461448
$this->_model->setParameters(['behavior' => Import::BEHAVIOR_ADD_UPDATE]);
462449

463-
$this->configShare->expects($this->once())
464-
->method('isGlobalScope')
465-
->willReturn(false);
466-
467450
if ($isValid) {
468451
$this->assertTrue($this->_model->validateRow($rowData, 0));
469452
} else {
@@ -481,16 +464,6 @@ public function testValidateRowForUpdate(array $rowData, array $errors, $isValid
481464
*/
482465
public function testValidateRowForUpdateGlobalCustomer(array $rowData, array $errors, $isValid = false)
483466
{
484-
$this->_model->setParameters(['behavior' => Import::BEHAVIOR_ADD_UPDATE]);
485-
486-
$this->configShare->expects($this->once())
487-
->method('isGlobalScope')
488-
->willReturn(true);
489-
490-
$this->customerStorage->expects($this->once())
491-
->method('getCustomerIdByEmail')
492-
->willReturn(1);
493-
494467
if ($isValid) {
495468
$this->assertTrue($this->_model->validateRow($rowData, 0));
496469
} else {

0 commit comments

Comments
 (0)