Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
engcom-Charlie committed May 22, 2020
1 parent 3f1099e commit 4afc05f
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 223 deletions.
49 changes: 49 additions & 0 deletions app/code/Magento/Customer/Model/Plugin/UpdateCustomerId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Customer\Model\Plugin;

use Magento\Framework\Webapi\Rest\Request as RestRequest;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;

/**
* Update customer id from request param
*/
class UpdateCustomerId
{
/**
* @var RestRequest $request
*/
private $request;

/**
* @param RestRequest $request
*/
public function __construct(RestRequest $request)
{
$this->request = $request;
}

/**
* Update customer id from request if exist
*
* @param CustomerRepositoryInterface $customerRepository
* @param CustomerInterface $customer
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeSave(CustomerRepositoryInterface $customerRepository, CustomerInterface $customer): void
{
$cartId = $this->request->getParam('customerId');

if ($cartId) {
$customer->setId($cartId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\EntityManager\HydratorInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Store\Model\StoreManagerInterface;

Expand Down Expand Up @@ -119,6 +120,11 @@ class CustomerRepository implements CustomerRepositoryInterface
*/
private $delegatedStorage;

/**
* @var HydratorInterface
*/
private $hydrator;

/**
* @param CustomerFactory $customerFactory
* @param CustomerSecureFactory $customerSecureFactory
Expand All @@ -136,6 +142,7 @@ class CustomerRepository implements CustomerRepositoryInterface
* @param CollectionProcessorInterface $collectionProcessor
* @param NotificationStorage $notificationStorage
* @param DelegatedStorage|null $delegatedStorage
* @param HydratorInterface|null $hydrator
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -154,7 +161,8 @@ public function __construct(
JoinProcessorInterface $extensionAttributesJoinProcessor,
CollectionProcessorInterface $collectionProcessor,
NotificationStorage $notificationStorage,
DelegatedStorage $delegatedStorage = null
DelegatedStorage $delegatedStorage = null,
?HydratorInterface $hydrator = null
) {
$this->customerFactory = $customerFactory;
$this->customerSecureFactory = $customerSecureFactory;
Expand All @@ -172,6 +180,7 @@ public function __construct(
$this->collectionProcessor = $collectionProcessor;
$this->notificationStorage = $notificationStorage;
$this->delegatedStorage = $delegatedStorage ?? ObjectManager::getInstance()->get(DelegatedStorage::class);
$this->hydrator = $hydrator ?: ObjectManager::getInstance()->get(HydratorInterface::class);
}

/**
Expand All @@ -185,6 +194,7 @@ public function __construct(
* @throws \Magento\Framework\Exception\LocalizedException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function save(CustomerInterface $customer, $passwordHash = null)
{
Expand All @@ -193,10 +203,11 @@ public function save(CustomerInterface $customer, $passwordHash = null)
$prevCustomerData = $prevCustomerDataArr = null;
if ($customer->getId()) {
$prevCustomerData = $this->getById($customer->getId());
$prevCustomerDataArr = $prevCustomerData->__toArray();
$prevCustomerDataArr = $this->hydrator->extract($prevCustomerData);
$customer = $this->hydrator->hydrate($prevCustomerData, $customer->__toArray());
}
/** @var $customer \Magento\Customer\Model\Data\Customer */
$customerArr = $customer->__toArray();
$customerArr = $this->hydrator->extract($customer);
$customer = $this->imageProcessor->save(
$customer,
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
Expand Down
Loading

0 comments on commit 4afc05f

Please sign in to comment.