Skip to content

Commit 6f918fa

Browse files
committed
#28481: GraphQl. updateCustomer allows to set any INT value in gender argument
1 parent f384190 commit 6f918fa

File tree

6 files changed

+209
-84
lines changed

6 files changed

+209
-84
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\Api;
9+
10+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11+
12+
/**
13+
* Interface for customer data validator
14+
*/
15+
interface ValidateCustomerDataInterface
16+
{
17+
/**
18+
* Validate customer data
19+
*
20+
* @param array $customerData
21+
* @throws GraphQlInputException
22+
*/
23+
public function execute(array $customerData): void;
24+
}

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

Lines changed: 13 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
namespace Magento\CustomerGraphQl\Model\Customer;
99

10-
use Magento\Customer\Api\CustomerMetadataInterface;
11-
use Magento\Customer\Model\Data\AttributeMetadata;
10+
use Magento\CustomerGraphQl\Api\ValidateCustomerDataInterface;
1211
use Magento\Framework\Exception\LocalizedException;
1312
use Magento\Framework\Exception\NoSuchEntityException;
1413
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -19,11 +18,6 @@
1918
*/
2019
class ValidateCustomerData
2120
{
22-
/**
23-
* @var CustomerMetadataInterface
24-
*/
25-
private $customerMetadata;
26-
2721
/**
2822
* Get allowed/required customer attributes
2923
*
@@ -36,21 +30,26 @@ class ValidateCustomerData
3630
*/
3731
private $emailAddressValidator;
3832

33+
/**
34+
* @var ValidateCustomerDataInterface[]
35+
*/
36+
private $validators = [];
37+
3938
/**
4039
* ValidateCustomerData constructor.
4140
*
4241
* @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes
4342
* @param EmailAddressValidator $emailAddressValidator
44-
* @param CustomerMetadataInterface $customerMetadata
43+
* @param array $validators
4544
*/
4645
public function __construct(
4746
GetAllowedCustomerAttributes $getAllowedCustomerAttributes,
4847
EmailAddressValidator $emailAddressValidator,
49-
CustomerMetadataInterface $customerMetadata
48+
$validators = []
5049
) {
5150
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes;
5251
$this->emailAddressValidator = $emailAddressValidator;
53-
$this->customerMetadata = $customerMetadata;
52+
$this->validators = $validators;
5453
}
5554

5655
/**
@@ -61,81 +60,11 @@ public function __construct(
6160
* @throws LocalizedException
6261
* @throws NoSuchEntityException
6362
*/
64-
public function execute(array $customerData): void
65-
{
66-
$this->validateRequiredArguments($customerData);
67-
$this->validateEmail($customerData);
68-
$this->validateGender($customerData);
69-
}
70-
71-
/**
72-
* Validate required attributes
73-
*
74-
* @param array $customerData
75-
* @throws GraphQlInputException
76-
*/
77-
private function validateRequiredArguments(array $customerData): void
63+
public function execute(array $customerData)
7864
{
79-
$attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData));
80-
$errorInput = [];
81-
82-
foreach ($attributes as $attributeInfo) {
83-
if ($attributeInfo->getIsRequired()
84-
&& (!isset($customerData[$attributeInfo->getAttributeCode()])
85-
|| $customerData[$attributeInfo->getAttributeCode()] == '')
86-
) {
87-
$errorInput[] = $attributeInfo->getDefaultFrontendLabel();
88-
}
89-
}
90-
91-
if ($errorInput) {
92-
throw new GraphQlInputException(
93-
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
94-
);
95-
}
96-
}
97-
98-
/**
99-
* Validate an email
100-
*
101-
* @param array $customerData
102-
* @throws GraphQlInputException
103-
*/
104-
private function validateEmail(array $customerData): void
105-
{
106-
if (isset($customerData['email']) && !$this->emailAddressValidator->isValid($customerData['email'])) {
107-
throw new GraphQlInputException(
108-
__('"%1" is not a valid email address.', $customerData['email'])
109-
);
110-
}
111-
}
112-
113-
/**
114-
* Validate gender value
115-
*
116-
* @param array $customerData
117-
* @throws GraphQlInputException
118-
* @throws LocalizedException
119-
* @throws NoSuchEntityException
120-
*/
121-
private function validateGender(array $customerData): void
122-
{
123-
if (isset($customerData['gender']) && $customerData['gender']) {
124-
/** @var AttributeMetadata $genderData */
125-
$options = $this->customerMetadata->getAttributeMetadata('gender')->getOptions();
126-
127-
$isValid = false;
128-
foreach ($options as $optionData) {
129-
if ($optionData->getValue() && $optionData->getValue() == $customerData['gender']) {
130-
$isValid = true;
131-
}
132-
}
133-
134-
if (!$isValid) {
135-
throw new GraphQlInputException(
136-
__('"%1" is not a valid gender value.', $customerData['gender'])
137-
);
138-
}
65+
/** @var ValidateCustomerDataInterface $validator */
66+
foreach ($this->validators as $validator) {
67+
$validator->execute($customerData);
13968
}
14069
}
14170
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\ValidateCustomerData;
9+
10+
use Magento\CustomerGraphQl\Api\ValidateCustomerDataInterface;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\Framework\Validator\EmailAddress as EmailAddressValidator;
13+
14+
/**
15+
* Validates an email
16+
*/
17+
class ValidateEmail implements ValidateCustomerDataInterface
18+
{
19+
/**
20+
* @var EmailAddressValidator
21+
*/
22+
private $emailAddressValidator;
23+
24+
/**
25+
* ValidateEmail constructor.
26+
*
27+
* @param EmailAddressValidator $emailAddressValidator
28+
*/
29+
public function __construct(EmailAddressValidator $emailAddressValidator)
30+
{
31+
$this->emailAddressValidator = $emailAddressValidator;
32+
}
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function execute(array $customerData): void
38+
{
39+
if (isset($customerData['email']) && !$this->emailAddressValidator->isValid($customerData['email'])) {
40+
throw new GraphQlInputException(
41+
__('"%1" is not a valid email address.', $customerData['email'])
42+
);
43+
}
44+
}
45+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\ValidateCustomerData;
9+
10+
use Magento\Customer\Api\CustomerMetadataInterface;
11+
use Magento\Customer\Model\Data\AttributeMetadata;
12+
use Magento\CustomerGraphQl\Api\ValidateCustomerDataInterface;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
15+
/**
16+
* Validates gender value
17+
*/
18+
class ValidateGender implements ValidateCustomerDataInterface
19+
{
20+
/**
21+
* @var CustomerMetadataInterface
22+
*/
23+
private $customerMetadata;
24+
25+
/**
26+
* ValidateGender constructor.
27+
*
28+
* @param CustomerMetadataInterface $customerMetadata
29+
*/
30+
public function __construct(CustomerMetadataInterface $customerMetadata)
31+
{
32+
$this->customerMetadata = $customerMetadata;
33+
}
34+
35+
/**
36+
* @inheritDoc
37+
*/
38+
public function execute(array $customerData): void
39+
{
40+
if (isset($customerData['gender']) && $customerData['gender']) {
41+
/** @var AttributeMetadata $genderData */
42+
$options = $this->customerMetadata->getAttributeMetadata('gender')->getOptions();
43+
44+
$isValid = false;
45+
foreach ($options as $optionData) {
46+
if ($optionData->getValue() && $optionData->getValue() == $customerData['gender']) {
47+
$isValid = true;
48+
}
49+
}
50+
51+
if (!$isValid) {
52+
throw new GraphQlInputException(
53+
__('"%1" is not a valid gender value.', $customerData['gender'])
54+
);
55+
}
56+
}
57+
}
58+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\ValidateCustomerData;
9+
10+
use Magento\CustomerGraphQl\Api\ValidateCustomerDataInterface;
11+
use Magento\CustomerGraphQl\Model\Customer\GetAllowedCustomerAttributes;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
14+
/**
15+
* Validates required attributes
16+
*/
17+
class ValidateRequiredArguments implements ValidateCustomerDataInterface
18+
{
19+
/**
20+
* Get allowed/required customer attributes
21+
*
22+
* @var GetAllowedCustomerAttributes
23+
*/
24+
private $getAllowedCustomerAttributes;
25+
26+
/**
27+
* ValidateRequiredArguments constructor.
28+
*
29+
* @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes
30+
*/
31+
public function __construct(GetAllowedCustomerAttributes $getAllowedCustomerAttributes)
32+
{
33+
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes;
34+
}
35+
36+
/**
37+
* @inheritDoc
38+
*/
39+
public function execute(array $customerData): void
40+
{
41+
$attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData));
42+
$errorInput = [];
43+
44+
foreach ($attributes as $attributeInfo) {
45+
if ($attributeInfo->getIsRequired()
46+
&& (!isset($customerData[$attributeInfo->getAttributeCode()])
47+
|| $customerData[$attributeInfo->getAttributeCode()] == '')
48+
) {
49+
$errorInput[] = $attributeInfo->getDefaultFrontendLabel();
50+
}
51+
}
52+
53+
if ($errorInput) {
54+
throw new GraphQlInputException(
55+
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
56+
);
57+
}
58+
}
59+
}

app/code/Magento/CustomerGraphQl/etc/graphql/di.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,14 @@
2929
</argument>
3030
</arguments>
3131
</type>
32+
<!-- Validate input customer data -->
33+
<type name="Magento\CustomerGraphQl\Model\Customer\ValidateCustomerData">
34+
<arguments>
35+
<argument name="validators" xsi:type="array">
36+
<item name="validateRequiredArguments" xsi:type="object">Magento\CustomerGraphQl\Model\Customer\ValidateCustomerData\ValidateRequiredArguments</item>
37+
<item name="validateEmail" xsi:type="object">Magento\CustomerGraphQl\Model\Customer\ValidateCustomerData\ValidateEmail</item>
38+
<item name="validateGender" xsi:type="object">Magento\CustomerGraphQl\Model\Customer\ValidateCustomerData\ValidateGender</item>
39+
</argument>
40+
</arguments>
41+
</type>
3242
</config>

0 commit comments

Comments
 (0)