Skip to content

Commit 9f1ba1a

Browse files
committed
magento#271: [My Account] Refactoring customer attributes validation
1 parent 9b5b0a1 commit 9f1ba1a

File tree

4 files changed

+103
-78
lines changed

4 files changed

+103
-78
lines changed

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

+5-30
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ class CreateCustomerAccount
4646
private $changeSubscriptionStatus;
4747

4848
/**
49-
* @var GetAllowedCustomerAttributes
49+
* @var ValidateCustomerData
5050
*/
51-
private $getAllowedCustomerAttributes;
51+
private $validateCustomerData;
5252

5353
/**
5454
* @param DataObjectHelper $dataObjectHelper
@@ -64,14 +64,14 @@ public function __construct(
6464
StoreManagerInterface $storeManager,
6565
AccountManagementInterface $accountManagement,
6666
ChangeSubscriptionStatus $changeSubscriptionStatus,
67-
GetAllowedCustomerAttributes $getAllowedCustomerAttributes
67+
ValidateCustomerData $validateCustomerData
6868
) {
6969
$this->dataObjectHelper = $dataObjectHelper;
7070
$this->customerFactory = $customerFactory;
7171
$this->accountManagement = $accountManagement;
7272
$this->storeManager = $storeManager;
7373
$this->changeSubscriptionStatus = $changeSubscriptionStatus;
74-
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes;
74+
$this->validateCustomerData = $validateCustomerData;
7575
}
7676

7777
/**
@@ -104,7 +104,7 @@ public function execute(array $data): CustomerInterface
104104
*/
105105
private function createAccount(array $data): CustomerInterface
106106
{
107-
$this->validateData($data);
107+
$this->validateCustomerData->execute($data);
108108
$customerDataObject = $this->customerFactory->create();
109109
$this->dataObjectHelper->populateWithArray(
110110
$customerDataObject,
@@ -118,29 +118,4 @@ private function createAccount(array $data): CustomerInterface
118118
$password = array_key_exists('password', $data) ? $data['password'] : null;
119119
return $this->accountManagement->createAccount($customerDataObject, $password);
120120
}
121-
122-
/**
123-
* @param array $customerData
124-
* @return void
125-
* @throws GraphQlInputException
126-
*/
127-
public function validateData(array $customerData): void
128-
{
129-
$attributes = $this->getAllowedCustomerAttributes->execute();
130-
$errorInput = [];
131-
132-
foreach ($attributes as $attributeName => $attributeInfo) {
133-
if ($attributeInfo->getIsRequired()
134-
&& (isset($customerData[$attributeName]) && empty($customerData[$attributeName]))
135-
) {
136-
$errorInput[] = $attributeName;
137-
}
138-
}
139-
140-
if ($errorInput) {
141-
throw new GraphQlInputException(
142-
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
143-
);
144-
}
145-
}
146121
}

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

+39-14
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,67 @@
88
namespace Magento\CustomerGraphQl\Model\Customer;
99

1010
use Magento\Customer\Api\CustomerMetadataManagementInterface;
11-
use Magento\Eav\Model\Config;
11+
use Magento\Eav\Model\AttributeRepository;
1212
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
13+
use Magento\Framework\Api\SearchCriteriaBuilder;
14+
use Magento\Framework\Exception\InputException;
15+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1316

1417
/**
1518
* Get allowed address attributes
1619
*/
1720
class GetAllowedCustomerAttributes
1821
{
1922
/**
20-
* @var Config
23+
* @var AttributeRepository
2124
*/
22-
private $eavConfig;
25+
private $attributeRepository;
2326

2427
/**
25-
* @param Config $eavConfig
28+
* @var SearchCriteriaBuilder
2629
*/
27-
public function __construct(Config $eavConfig)
28-
{
29-
$this->eavConfig = $eavConfig;
30+
private $searchCriteriaBuilder;
31+
32+
/**
33+
* @param AttributeRepository $attributeRepository
34+
*/
35+
public function __construct(
36+
AttributeRepository $attributeRepository,
37+
SearchCriteriaBuilder $searchCriteriaBuilder
38+
) {
39+
$this->attributeRepository = $attributeRepository;
40+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
3041
}
3142

3243
/**
33-
* Get allowed address attributes
44+
* Get allowed customer attributes
3445
*
46+
* @param array $attributeKeys
47+
* @throws GraphQlInputException
3548
* @return AbstractAttribute[]
3649
*/
37-
public function execute(): array
50+
public function execute($attributeKeys): array
3851
{
39-
$attributes = $this->eavConfig->getEntityAttributes(
40-
CustomerMetadataManagementInterface::ENTITY_TYPE_CUSTOMER
41-
);
42-
foreach ($attributes as $attributeCode => $attribute) {
52+
$this->searchCriteriaBuilder->addFilter('attribute_code', $attributeKeys, 'in');
53+
$searchCriteria = $this->searchCriteriaBuilder->create();
54+
try {
55+
$attributesSearchResult = $this->attributeRepository->getList(
56+
CustomerMetadataManagementInterface::ENTITY_TYPE_CUSTOMER,
57+
$searchCriteria
58+
);
59+
} catch (InputException $exception) {
60+
throw new GraphQlInputException(__($exception->getMessage()));
61+
}
62+
63+
/** @var AbstractAttribute[] $attributes */
64+
$attributes = $attributesSearchResult->getItems();
65+
66+
foreach ($attributes as $index => $attribute) {
4367
if (false === $attribute->getIsVisibleOnFront()) {
44-
unset($attributes[$attributeCode]);
68+
unset($attributes[$index]);
4569
}
4670
}
71+
4772
return $attributes;
4873
}
4974
}

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

+8-34
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ class UpdateCustomerAccount
4545
private $changeSubscriptionStatus;
4646

4747
/**
48-
* @var GetAllowedCustomerAttributes
48+
* @var ValidateCustomerData
4949
*/
50-
private $getAllowedCustomerAttributes;
50+
private $validateCustomerData;
5151

5252
/**
5353
* @var array
@@ -60,7 +60,7 @@ class UpdateCustomerAccount
6060
* @param CheckCustomerPassword $checkCustomerPassword
6161
* @param DataObjectHelper $dataObjectHelper
6262
* @param ChangeSubscriptionStatus $changeSubscriptionStatus
63-
* @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes
63+
* @param ValidateCustomerData $validateCustomerData
6464
* @param array $restrictedKeys
6565
*/
6666
public function __construct(
@@ -69,7 +69,7 @@ public function __construct(
6969
CheckCustomerPassword $checkCustomerPassword,
7070
DataObjectHelper $dataObjectHelper,
7171
ChangeSubscriptionStatus $changeSubscriptionStatus,
72-
GetAllowedCustomerAttributes $getAllowedCustomerAttributes,
72+
ValidateCustomerData $validateCustomerData,
7373
array $restrictedKeys = []
7474
) {
7575
$this->saveCustomer = $saveCustomer;
@@ -78,18 +78,17 @@ public function __construct(
7878
$this->dataObjectHelper = $dataObjectHelper;
7979
$this->restrictedKeys = $restrictedKeys;
8080
$this->changeSubscriptionStatus = $changeSubscriptionStatus;
81-
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes;
81+
$this->validateCustomerData = $validateCustomerData;
8282
}
8383

8484
/**
85-
* Update customer account data
86-
*
8785
* @param CustomerInterface $customer
8886
* @param array $data
89-
* @return void
9087
* @throws GraphQlAlreadyExistsException
9188
* @throws GraphQlAuthenticationException
9289
* @throws GraphQlInputException
90+
* @throws \Magento\Framework\Exception\NoSuchEntityException
91+
* @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException
9392
*/
9493
public function execute(CustomerInterface $customer, array $data): void
9594
{
@@ -101,7 +100,7 @@ public function execute(CustomerInterface $customer, array $data): void
101100
$this->checkCustomerPassword->execute($data['password'], (int)$customer->getId());
102101
$customer->setEmail($data['email']);
103102
}
104-
$this->validateData($data);
103+
$this->validateCustomerData->execute($data);
105104
$filteredData = array_diff_key($data, array_flip($this->restrictedKeys));
106105
$this->dataObjectHelper->populateWithArray($customer, $filteredData, CustomerInterface::class);
107106

@@ -113,29 +112,4 @@ public function execute(CustomerInterface $customer, array $data): void
113112
$this->changeSubscriptionStatus->execute((int)$customer->getId(), (bool)$data['is_subscribed']);
114113
}
115114
}
116-
117-
/**
118-
* @param array $customerData
119-
* @return void
120-
* @throws GraphQlInputException
121-
*/
122-
public function validateData(array $customerData): void
123-
{
124-
$attributes = $this->getAllowedCustomerAttributes->execute();
125-
$errorInput = [];
126-
127-
foreach ($attributes as $attributeName => $attributeInfo) {
128-
if ($attributeInfo->getIsRequired()
129-
&& (isset($customerData[$attributeName]) && empty($customerData[$attributeName]))
130-
) {
131-
$errorInput[] = $attributeName;
132-
}
133-
}
134-
135-
if ($errorInput) {
136-
throw new GraphQlInputException(
137-
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
138-
);
139-
}
140-
}
141115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Customer;
9+
10+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11+
12+
class ValidateCustomerData
13+
{
14+
/**
15+
* @var GetAllowedCustomerAttributes
16+
*/
17+
private $getAllowedCustomerAttributes;
18+
19+
/**
20+
* ValidateCustomerData constructor.
21+
*
22+
* @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes
23+
*/
24+
public function __construct(GetAllowedCustomerAttributes $getAllowedCustomerAttributes)
25+
{
26+
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes;
27+
}
28+
29+
/**
30+
* @param array $customerData
31+
* @return void
32+
* @throws GraphQlInputException
33+
*/
34+
public function execute(array $customerData): void
35+
{
36+
$attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData));
37+
$errorInput = [];
38+
39+
foreach ($attributes as $attributeName => $attributeInfo) {
40+
if ($attributeInfo->getIsRequired() && empty($customerData[$attributeName])) {
41+
$errorInput[] = $attributeName;
42+
}
43+
}
44+
45+
if ($errorInput) {
46+
throw new GraphQlInputException(
47+
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
48+
);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)