Skip to content

Commit 8459b17

Browse files
authored
Merge pull request #9202 from adobe-commerce-tier-4/Tier4-Kings-PR-08-23-2024
[Support Tier-4-Kings glo02433] 08.23.2024 Regular delivery of bugfixes and improvements
2 parents d06337a + d382b03 commit 8459b17

File tree

14 files changed

+585
-129
lines changed

14 files changed

+585
-129
lines changed

app/code/Magento/CatalogGraphQl/Plugin/ProductAttributeSortInput.php

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,30 @@
2323
use GraphQL\Language\Visitor;
2424
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
2525
use Magento\GraphQl\Model\Query\ContextInterface;
26+
use Magento\Framework\App\RequestInterface;
27+
use Magento\Framework\Serialize\SerializerInterface;
2628

2729
class ProductAttributeSortInput
2830
{
31+
/**
32+
* @var RequestInterface
33+
*/
34+
private $request;
35+
36+
/**
37+
* @var SerializerInterface
38+
*/
39+
private $jsonSerializer;
40+
41+
/**
42+
* @param RequestInterface $request
43+
* @param SerializerInterface $jsonSerializer
44+
*/
45+
public function __construct(RequestInterface $request, SerializerInterface $jsonSerializer)
46+
{
47+
$this->request = $request;
48+
$this->jsonSerializer = $jsonSerializer;
49+
}
2950
/**
3051
* Plugin to preserve the original order of sort fields
3152
*
@@ -47,43 +68,83 @@ public function beforeResolve(
4768
array $value = null,
4869
array $args = null
4970
): array {
71+
72+
$data = $this->getDataFromRequest($this->request);
5073
if (isset($args['sort'])) {
51-
$args['sort'] = $this->getSortFieldsOrder($info, $args['sort']);
74+
$args['sort'] = $this->getSortFieldsOrder(
75+
$info,
76+
$args['sort'],
77+
isset($data['variables']['sort']) ? array_keys($data['variables']['sort']) : null
78+
);
5279
}
5380
return [$field, $context, $info, $value, $args];
5481
}
5582

83+
/**
84+
* Get data from request body or query string
85+
*
86+
* @param RequestInterface $request
87+
* @return array
88+
*/
89+
private function getDataFromRequest(RequestInterface $request): array
90+
{
91+
$data = [];
92+
if ($request->isPost()) {
93+
$data = $this->jsonSerializer->unserialize($request->getContent());
94+
} elseif ($request->isGet()) {
95+
$data = $request->getParams();
96+
$data['variables'] = isset($data['variables']) ?
97+
$this->jsonSerializer->unserialize($data['variables']) : null;
98+
$data['variables'] = is_array($data['variables']) ?
99+
$data['variables'] : null;
100+
}
101+
return $data;
102+
}
103+
56104
/**
57105
* Get sort fields in the original order
58106
*
59107
* @param ResolveInfo $info
60108
* @param array $sortFields
109+
* @param array|null $requestSortFields
61110
* @return array
62111
* @throws \Exception
63112
*/
64-
private function getSortFieldsOrder(ResolveInfo $info, array $sortFields)
113+
private function getSortFieldsOrder(ResolveInfo $info, array $sortFields, ?array $requestSortFields): array
65114
{
66115
$sortFieldsOriginal = [];
67116
Visitor::visit(
68117
$info->operation,
69118
[
70119
'enter' => [
71-
NodeKind::ARGUMENT => function (Node $node) use (&$sortFieldsOriginal, $sortFields) {
120+
NodeKind::ARGUMENT => function (Node $node) use (
121+
&$sortFieldsOriginal,
122+
$sortFields,
123+
$requestSortFields
124+
) {
72125
if ($node->name->value === 'sort') {
73-
Visitor::visit(
74-
$node->value,
75-
[
76-
'enter' => [
77-
NodeKind::OBJECT_FIELD =>
78-
function (Node $node) use (&$sortFieldsOriginal, $sortFields) {
79-
if (isset($sortFields[$node->name->value])) {
80-
$sortFieldsOriginal[$node->name->value] =
81-
$sortFields[$node->name->value];
126+
if (isset($requestSortFields)) {
127+
foreach ($requestSortFields as $fieldName) {
128+
if (isset($sortFields[$fieldName])) {
129+
$sortFieldsOriginal[$fieldName] = $sortFields[$fieldName];
130+
}
131+
}
132+
} else {
133+
Visitor::visit(
134+
$node->value,
135+
[
136+
'enter' => [
137+
NodeKind::OBJECT_FIELD =>
138+
function (Node $node) use (&$sortFieldsOriginal, $sortFields) {
139+
if (isset($sortFields[$node->name->value])) {
140+
$sortFieldsOriginal[$node->name->value] =
141+
$sortFields[$node->name->value];
142+
}
82143
}
83-
}
144+
]
84145
]
85-
]
86-
);
146+
);
147+
}
87148
return Visitor::stop();
88149
}
89150
}

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
*

0 commit comments

Comments
 (0)