Skip to content

Commit da8d6ac

Browse files
committed
#271: [My Account] Refactoring the addition of required attributes to validation of customer creation
1 parent 4382c3a commit da8d6ac

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\Api\DataObjectHelper;
1414
use Magento\Framework\Exception\LocalizedException;
1515
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
16+
use Magento\Framework\Reflection\DataObjectProcessor;
1617
use Magento\Store\Model\StoreManagerInterface;
1718

1819
/**
@@ -50,6 +51,11 @@ class CreateCustomerAccount
5051
*/
5152
private $validateCustomerData;
5253

54+
/**
55+
* @var DataObjectProcessor
56+
*/
57+
private $dataObjectProcessor;
58+
5359
/**
5460
* CreateCustomerAccount constructor.
5561
*
@@ -58,6 +64,7 @@ class CreateCustomerAccount
5864
* @param StoreManagerInterface $storeManager
5965
* @param AccountManagementInterface $accountManagement
6066
* @param ChangeSubscriptionStatus $changeSubscriptionStatus
67+
* @param DataObjectProcessor $dataObjectProcessor
6168
* @param ValidateCustomerData $validateCustomerData
6269
*/
6370
public function __construct(
@@ -66,6 +73,7 @@ public function __construct(
6673
StoreManagerInterface $storeManager,
6774
AccountManagementInterface $accountManagement,
6875
ChangeSubscriptionStatus $changeSubscriptionStatus,
76+
DataObjectProcessor $dataObjectProcessor,
6977
ValidateCustomerData $validateCustomerData
7078
) {
7179
$this->dataObjectHelper = $dataObjectHelper;
@@ -74,6 +82,7 @@ public function __construct(
7482
$this->storeManager = $storeManager;
7583
$this->changeSubscriptionStatus = $changeSubscriptionStatus;
7684
$this->validateCustomerData = $validateCustomerData;
85+
$this->dataObjectProcessor = $dataObjectProcessor;
7786
}
7887

7988
/**
@@ -106,8 +115,16 @@ public function execute(array $data): CustomerInterface
106115
*/
107116
private function createAccount(array $data): CustomerInterface
108117
{
109-
$this->validateCustomerData->execute($data, true);
110118
$customerDataObject = $this->customerFactory->create();
119+
/**
120+
* Add required attributes for customer entity
121+
*/
122+
$requiredDataAttributes = $this->dataObjectProcessor->buildOutputDataArray(
123+
$customerDataObject,
124+
CustomerInterface::class
125+
);
126+
$data = array_merge($requiredDataAttributes, $data);
127+
$this->validateCustomerData->execute($data);
111128
$this->dataObjectHelper->populateWithArray(
112129
$customerDataObject,
113130
$data,

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,13 @@ public function __construct(
6666
* Get allowed customer attributes
6767
*
6868
* @param array $attributeKeys
69-
* @param bool $addRequiredAttributes
7069
*
7170
* @throws GraphQlInputException
7271
*
7372
* @return AbstractAttribute[]
7473
*/
75-
public function execute($attributeKeys, $addRequiredAttributes = false): array
74+
public function execute($attributeKeys): array
7675
{
77-
/**
78-
* Add required attributes for customer entity to passed attribute keys
79-
*/
80-
if ($addRequiredAttributes) {
81-
/** @var CustomerInterface $customerDataDummy */
82-
$customerDataDummy = $this->customerDataFactory->create();
83-
$requiredDataAttributes = $this->dataObjectProcessor->buildOutputDataArray(
84-
$customerDataDummy,
85-
CustomerInterface::class
86-
);
87-
$attributeKeys = array_merge($attributeKeys, array_keys($requiredDataAttributes));
88-
}
89-
9076
$this->searchCriteriaBuilder->addFilter('attribute_code', $attributeKeys, 'in');
9177
$searchCriteria = $this->searchCriteriaBuilder->create();
9278
try {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ public function __construct(GetAllowedCustomerAttributes $getAllowedCustomerAttr
3535
* Validate customer data
3636
*
3737
* @param array $customerData
38-
* @param bool $addRequiredAttributes
3938
*
4039
* @return void
4140
*
4241
* @throws GraphQlInputException
4342
*/
44-
public function execute(array $customerData, $addRequiredAttributes = false): void
43+
public function execute(array $customerData): void
4544
{
46-
$attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData), $addRequiredAttributes);
45+
$attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData));
4746
$errorInput = [];
4847

4948
foreach ($attributes as $attributeInfo) {

0 commit comments

Comments
 (0)