Skip to content

Commit a83afb7

Browse files
committed
Refactor data setting and restriction di
* update how customer object is updated * add di to restrict updating defined properties
1 parent 8e8ca79 commit a83afb7

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerData.php

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1414
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1515
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\Customer\Api\Data\CustomerInterface;
17+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
18+
use Magento\Framework\Api\DataObjectHelper;
19+
use Magento\Framework\Reflection\DataObjectProcessor;
1620

1721
/**
1822
* Update customer data
1923
*/
2024
class UpdateCustomerData
2125
{
22-
private const RESTRICTED_DATA_KEYS = ['email', 'password'];
23-
2426
/**
2527
* @var CustomerRepositoryInterface
2628
*/
@@ -36,19 +38,51 @@ class UpdateCustomerData
3638
*/
3739
private $checkCustomerPassword;
3840

41+
/**
42+
* @var CustomerInterfaceFactory
43+
*/
44+
private $customerFactory;
45+
46+
/**
47+
* @var DataObjectHelper
48+
*/
49+
private $dataObjectHelper;
50+
51+
/**
52+
* @var DataObjectProcessor
53+
*/
54+
private $dataObjectProcessor;
55+
56+
/**
57+
* @var array
58+
*/
59+
private $restrictedKeys;
60+
3961
/**
4062
* @param CustomerRepositoryInterface $customerRepository
4163
* @param StoreManagerInterface $storeManager
4264
* @param CheckCustomerPassword $checkCustomerPassword
65+
* @param CustomerInterfaceFactory $customerFactory
66+
* @param DataObjectHelper $dataObjectHelper
67+
* @param DataObjectProcessor $dataObjectProcessor
68+
* @param array $restrictedKeys
4369
*/
4470
public function __construct(
4571
CustomerRepositoryInterface $customerRepository,
4672
StoreManagerInterface $storeManager,
47-
CheckCustomerPassword $checkCustomerPassword
73+
CheckCustomerPassword $checkCustomerPassword,
74+
CustomerInterfaceFactory $customerFactory,
75+
DataObjectHelper $dataObjectHelper,
76+
DataObjectProcessor $dataObjectProcessor,
77+
array $restrictedKeys = []
4878
) {
4979
$this->customerRepository = $customerRepository;
5080
$this->storeManager = $storeManager;
5181
$this->checkCustomerPassword = $checkCustomerPassword;
82+
$this->customerFactory = $customerFactory;
83+
$this->dataObjectHelper = $dataObjectHelper;
84+
$this->dataObjectProcessor = $dataObjectProcessor;
85+
$this->restrictedKeys = $restrictedKeys;
5286
}
5387

5488
/**
@@ -64,15 +98,17 @@ public function __construct(
6498
public function execute(int $customerId, array $data): void
6599
{
66100
$customer = $this->customerRepository->getById($customerId);
67-
$newCustomerData = array_diff_key($data, array_flip(static::RESTRICTED_DATA_KEYS));
101+
$newData = array_diff_key($data, array_flip($this->restrictedKeys));
68102

69-
foreach ($newCustomerData as $key => $value) {
70-
$setterMethod = 'set' . ucwords($key, '_');
71-
if (!method_exists($customer, $setterMethod)) {
72-
continue;
73-
}
74-
$customer->{$setterMethod}($value);
75-
}
103+
$oldData = $this->dataObjectProcessor->buildOutputDataArray($customer, CustomerInterface::class);
104+
$newData = array_merge($oldData, $newData);
105+
106+
$customer = $this->customerFactory->create();
107+
$this->dataObjectHelper->populateWithArray(
108+
$customer,
109+
$newData,
110+
CustomerInterface::class
111+
);
76112

77113
if (isset($data['email']) && $customer->getEmail() !== $data['email']) {
78114
if (!isset($data['password']) || empty($data['password'])) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\CustomerGraphQl\Model\Customer\UpdateCustomerData">
10+
<arguments>
11+
<argument name="restrictedKeys" xsi:type="array">
12+
<item name="email" xsi:type="const">Magento\Customer\Api\Data\CustomerInterface::EMAIL</item>
13+
</argument>
14+
</arguments>
15+
</type>
16+
</config>

0 commit comments

Comments
 (0)