Skip to content

Commit cdbf988

Browse files
improve fix
1 parent d6d15d7 commit cdbf988

File tree

5 files changed

+226
-200
lines changed

5 files changed

+226
-200
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Customer\Model\Plugin;
10+
11+
use Magento\Framework\Webapi\Rest\Request as RestRequest;
12+
use Magento\Customer\Api\Data\CustomerInterface;
13+
use Magento\Customer\Api\CustomerRepositoryInterface;
14+
15+
/**
16+
* Update customer by id from request param
17+
*/
18+
class UpdateCustomer
19+
{
20+
/**
21+
* @var RestRequest
22+
*/
23+
private $request;
24+
25+
/**
26+
* @param RestRequest $request
27+
*/
28+
public function __construct(RestRequest $request)
29+
{
30+
$this->request = $request;
31+
}
32+
33+
/**
34+
* Update customer by id from request if exist
35+
*
36+
* @param CustomerRepositoryInterface $customerRepository
37+
* @param CustomerInterface $customer
38+
* @param string|null $passwordHash
39+
* @return array
40+
*/
41+
public function beforeSave(
42+
CustomerRepositoryInterface $customerRepository,
43+
CustomerInterface $customer,
44+
?string $passwordHash = null
45+
): array {
46+
$customerId = $this->request->getParam('customerId');
47+
48+
if ($customerId) {
49+
$customer = $this->getUpdatedCustomer($customerRepository->getById($customerId), $customer);
50+
}
51+
52+
return [$customer, $passwordHash];
53+
}
54+
55+
/**
56+
* Return updated customer
57+
*
58+
* @param CustomerInterface $originCustomer
59+
* @param CustomerInterface $customer
60+
* @return CustomerInterface
61+
*/
62+
private function getUpdatedCustomer(
63+
CustomerInterface $originCustomer,
64+
CustomerInterface $customer
65+
): CustomerInterface {
66+
$newCustomer = clone $originCustomer;
67+
foreach ($customer->__toArray() as $name => $value) {
68+
if ($name === CustomerInterface::CUSTOM_ATTRIBUTES) {
69+
$value = $customer->getCustomAttributes();
70+
} elseif ($name === CustomerInterface::EXTENSION_ATTRIBUTES_KEY) {
71+
$value = $customer->getExtensionAttributes();
72+
} elseif ($name === CustomerInterface::KEY_ADDRESSES) {
73+
$value = $customer->getAddresses();
74+
}
75+
76+
$newCustomer->setData($name, $value);
77+
}
78+
79+
return $newCustomer;
80+
}
81+
}

app/code/Magento/Customer/Model/Plugin/UpdateCustomerId.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

app/code/Magento/Customer/Model/ResourceModel/CustomerRepository.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
2727
use Magento\Framework\Api\SearchCriteriaInterface;
2828
use Magento\Framework\App\ObjectManager;
29-
use Magento\Framework\EntityManager\HydratorInterface;
3029
use Magento\Framework\Event\ManagerInterface;
3130
use Magento\Store\Model\StoreManagerInterface;
3231

@@ -120,11 +119,6 @@ class CustomerRepository implements CustomerRepositoryInterface
120119
*/
121120
private $delegatedStorage;
122121

123-
/**
124-
* @var HydratorInterface
125-
*/
126-
private $hydrator;
127-
128122
/**
129123
* @param CustomerFactory $customerFactory
130124
* @param CustomerSecureFactory $customerSecureFactory
@@ -142,7 +136,6 @@ class CustomerRepository implements CustomerRepositoryInterface
142136
* @param CollectionProcessorInterface $collectionProcessor
143137
* @param NotificationStorage $notificationStorage
144138
* @param DelegatedStorage|null $delegatedStorage
145-
* @param HydratorInterface|null $hydrator
146139
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
147140
*/
148141
public function __construct(
@@ -161,8 +154,7 @@ public function __construct(
161154
JoinProcessorInterface $extensionAttributesJoinProcessor,
162155
CollectionProcessorInterface $collectionProcessor,
163156
NotificationStorage $notificationStorage,
164-
DelegatedStorage $delegatedStorage = null,
165-
?HydratorInterface $hydrator = null
157+
DelegatedStorage $delegatedStorage = null
166158
) {
167159
$this->customerFactory = $customerFactory;
168160
$this->customerSecureFactory = $customerSecureFactory;
@@ -180,7 +172,6 @@ public function __construct(
180172
$this->collectionProcessor = $collectionProcessor;
181173
$this->notificationStorage = $notificationStorage;
182174
$this->delegatedStorage = $delegatedStorage ?? ObjectManager::getInstance()->get(DelegatedStorage::class);
183-
$this->hydrator = $hydrator ?: ObjectManager::getInstance()->get(HydratorInterface::class);
184175
}
185176

186177
/**
@@ -194,7 +185,6 @@ public function __construct(
194185
* @throws \Magento\Framework\Exception\LocalizedException
195186
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
196187
* @SuppressWarnings(PHPMD.NPathComplexity)
197-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
198188
*/
199189
public function save(CustomerInterface $customer, $passwordHash = null)
200190
{
@@ -203,11 +193,10 @@ public function save(CustomerInterface $customer, $passwordHash = null)
203193
$prevCustomerData = $prevCustomerDataArr = null;
204194
if ($customer->getId()) {
205195
$prevCustomerData = $this->getById($customer->getId());
206-
$prevCustomerDataArr = $this->hydrator->extract($prevCustomerData);
207-
$customer = $this->hydrator->hydrate($prevCustomerData, $customer->__toArray());
196+
$prevCustomerDataArr = $prevCustomerData->__toArray();
208197
}
209198
/** @var $customer \Magento\Customer\Model\Data\Customer */
210-
$customerArr = $this->hydrator->extract($customer);
199+
$customerArr = $customer->__toArray();
211200
$customer = $this->imageProcessor->save(
212201
$customer,
213202
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,

0 commit comments

Comments
 (0)