Skip to content

Commit 4afc05f

Browse files
fix
1 parent 3f1099e commit 4afc05f

File tree

5 files changed

+286
-223
lines changed

5 files changed

+286
-223
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 id from request param
17+
*/
18+
class UpdateCustomerId
19+
{
20+
/**
21+
* @var RestRequest $request
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 id from request if exist
35+
*
36+
* @param CustomerRepositoryInterface $customerRepository
37+
* @param CustomerInterface $customer
38+
* @return void
39+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
40+
*/
41+
public function beforeSave(CustomerRepositoryInterface $customerRepository, CustomerInterface $customer): void
42+
{
43+
$cartId = $this->request->getParam('customerId');
44+
45+
if ($cartId) {
46+
$customer->setId($cartId);
47+
}
48+
}
49+
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
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;
2930
use Magento\Framework\Event\ManagerInterface;
3031
use Magento\Store\Model\StoreManagerInterface;
3132

@@ -119,6 +120,11 @@ class CustomerRepository implements CustomerRepositoryInterface
119120
*/
120121
private $delegatedStorage;
121122

123+
/**
124+
* @var HydratorInterface
125+
*/
126+
private $hydrator;
127+
122128
/**
123129
* @param CustomerFactory $customerFactory
124130
* @param CustomerSecureFactory $customerSecureFactory
@@ -136,6 +142,7 @@ class CustomerRepository implements CustomerRepositoryInterface
136142
* @param CollectionProcessorInterface $collectionProcessor
137143
* @param NotificationStorage $notificationStorage
138144
* @param DelegatedStorage|null $delegatedStorage
145+
* @param HydratorInterface|null $hydrator
139146
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
140147
*/
141148
public function __construct(
@@ -154,7 +161,8 @@ public function __construct(
154161
JoinProcessorInterface $extensionAttributesJoinProcessor,
155162
CollectionProcessorInterface $collectionProcessor,
156163
NotificationStorage $notificationStorage,
157-
DelegatedStorage $delegatedStorage = null
164+
DelegatedStorage $delegatedStorage = null,
165+
?HydratorInterface $hydrator = null
158166
) {
159167
$this->customerFactory = $customerFactory;
160168
$this->customerSecureFactory = $customerSecureFactory;
@@ -172,6 +180,7 @@ public function __construct(
172180
$this->collectionProcessor = $collectionProcessor;
173181
$this->notificationStorage = $notificationStorage;
174182
$this->delegatedStorage = $delegatedStorage ?? ObjectManager::getInstance()->get(DelegatedStorage::class);
183+
$this->hydrator = $hydrator ?: ObjectManager::getInstance()->get(HydratorInterface::class);
175184
}
176185

177186
/**
@@ -185,6 +194,7 @@ public function __construct(
185194
* @throws \Magento\Framework\Exception\LocalizedException
186195
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
187196
* @SuppressWarnings(PHPMD.NPathComplexity)
197+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
188198
*/
189199
public function save(CustomerInterface $customer, $passwordHash = null)
190200
{
@@ -193,10 +203,11 @@ public function save(CustomerInterface $customer, $passwordHash = null)
193203
$prevCustomerData = $prevCustomerDataArr = null;
194204
if ($customer->getId()) {
195205
$prevCustomerData = $this->getById($customer->getId());
196-
$prevCustomerDataArr = $prevCustomerData->__toArray();
206+
$prevCustomerDataArr = $this->hydrator->extract($prevCustomerData);
207+
$customer = $this->hydrator->hydrate($prevCustomerData, $customer->__toArray());
197208
}
198209
/** @var $customer \Magento\Customer\Model\Data\Customer */
199-
$customerArr = $customer->__toArray();
210+
$customerArr = $this->hydrator->extract($customer);
200211
$customer = $this->imageProcessor->save(
201212
$customer,
202213
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,

0 commit comments

Comments
 (0)