This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#271: [My Account] Refactoring customer attributes validation
- Loading branch information
Showing
4 changed files
with
103 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Customer; | ||
|
||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
|
||
class ValidateCustomerData | ||
{ | ||
/** | ||
* @var GetAllowedCustomerAttributes | ||
*/ | ||
private $getAllowedCustomerAttributes; | ||
|
||
/** | ||
* ValidateCustomerData constructor. | ||
* | ||
* @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes | ||
*/ | ||
public function __construct(GetAllowedCustomerAttributes $getAllowedCustomerAttributes) | ||
{ | ||
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes; | ||
} | ||
|
||
/** | ||
* @param array $customerData | ||
* @return void | ||
* @throws GraphQlInputException | ||
*/ | ||
public function execute(array $customerData): void | ||
{ | ||
$attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData)); | ||
$errorInput = []; | ||
|
||
foreach ($attributes as $attributeName => $attributeInfo) { | ||
if ($attributeInfo->getIsRequired() && empty($customerData[$attributeName])) { | ||
$errorInput[] = $attributeName; | ||
} | ||
} | ||
|
||
if ($errorInput) { | ||
throw new GraphQlInputException( | ||
__('Required parameters are missing: %1', [implode(', ', $errorInput)]) | ||
); | ||
} | ||
} | ||
} |