forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-5606: magento#271 [My Account] Add support of Customer attribu…
…tes magento#489 - Merge Pull Request magento/graphql-ce#489 from magento/graphql-ce:feature/271-Customer-Attributes-Validation - Merged commits: 1. dba9800 2. 7f6718d 3. 3735e91 4. 342675a 5. e177801 6. 9b5b0a1 7. 9f1ba1a 8. 670116c 9. 40e2363 10. d071027 11. b4db736 12. 4b7acdc 13. 48f1891 14. 3641627 15. ef078b6 16. 1c65121 17. cc69055 18. 7ee6a1a 19. f358267 20. eb48f44 21. 2eb7c15 22. 7ca7acc 23. 1c30336 24. 4382c3a 25. da8d6ac 26. 0b4c496 27. 5ae0633
- Loading branch information
Showing
6 changed files
with
277 additions
and
7 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
98 changes: 98 additions & 0 deletions
98
app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.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,98 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Customer; | ||
|
||
use Magento\Customer\Api\CustomerMetadataManagementInterface; | ||
use Magento\Customer\Api\Data\CustomerInterface; | ||
use Magento\Customer\Api\Data\CustomerInterfaceFactory; | ||
use Magento\Eav\Model\AttributeRepository; | ||
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Framework\Exception\InputException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\Reflection\DataObjectProcessor; | ||
|
||
/** | ||
* Get allowed address attributes | ||
*/ | ||
class GetAllowedCustomerAttributes | ||
{ | ||
/** | ||
* @var AttributeRepository | ||
*/ | ||
private $attributeRepository; | ||
|
||
/** | ||
* @var CustomerInterfaceFactory\ | ||
*/ | ||
private $customerDataFactory; | ||
|
||
/** | ||
* @var DataObjectProcessor | ||
*/ | ||
private $dataObjectProcessor; | ||
|
||
/** | ||
* @var SearchCriteriaBuilder | ||
*/ | ||
private $searchCriteriaBuilder; | ||
|
||
/** | ||
* GetAllowedCustomerAttributes constructor. | ||
* | ||
* @param AttributeRepository $attributeRepository | ||
* @param CustomerInterfaceFactory $customerDataFactory | ||
* @param DataObjectProcessor $dataObjectProcessor | ||
* @param SearchCriteriaBuilder $searchCriteriaBuilder | ||
*/ | ||
public function __construct( | ||
AttributeRepository $attributeRepository, | ||
CustomerInterfaceFactory $customerDataFactory, | ||
DataObjectProcessor $dataObjectProcessor, | ||
SearchCriteriaBuilder $searchCriteriaBuilder | ||
) { | ||
$this->attributeRepository = $attributeRepository; | ||
$this->customerDataFactory = $customerDataFactory; | ||
$this->dataObjectProcessor = $dataObjectProcessor; | ||
$this->searchCriteriaBuilder = $searchCriteriaBuilder; | ||
} | ||
|
||
/** | ||
* Get allowed customer attributes | ||
* | ||
* @param array $attributeKeys | ||
* | ||
* @throws GraphQlInputException | ||
* | ||
* @return AbstractAttribute[] | ||
*/ | ||
public function execute($attributeKeys): array | ||
{ | ||
$this->searchCriteriaBuilder->addFilter('attribute_code', $attributeKeys, 'in'); | ||
$searchCriteria = $this->searchCriteriaBuilder->create(); | ||
try { | ||
$attributesSearchResult = $this->attributeRepository->getList( | ||
CustomerMetadataManagementInterface::ENTITY_TYPE_CUSTOMER, | ||
$searchCriteria | ||
); | ||
} catch (InputException $exception) { | ||
throw new GraphQlInputException(__($exception->getMessage())); | ||
} | ||
|
||
/** @var AbstractAttribute[] $attributes */ | ||
$attributes = $attributesSearchResult->getItems(); | ||
|
||
foreach ($attributes as $index => $attribute) { | ||
if (false === $attribute->getIsVisibleOnFront()) { | ||
unset($attributes[$index]); | ||
} | ||
} | ||
|
||
return $attributes; | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
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,63 @@ | ||
<?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 | ||
*/ | ||
class ValidateCustomerData | ||
{ | ||
/** | ||
* Get allowed/required customer attributes | ||
* | ||
* @var GetAllowedCustomerAttributes | ||
*/ | ||
private $getAllowedCustomerAttributes; | ||
|
||
/** | ||
* ValidateCustomerData constructor. | ||
* | ||
* @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes | ||
*/ | ||
public function __construct(GetAllowedCustomerAttributes $getAllowedCustomerAttributes) | ||
{ | ||
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes; | ||
} | ||
|
||
/** | ||
* Validate customer data | ||
* | ||
* @param array $customerData | ||
* | ||
* @return void | ||
* | ||
* @throws GraphQlInputException | ||
*/ | ||
public function execute(array $customerData): void | ||
{ | ||
$attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData)); | ||
$errorInput = []; | ||
|
||
foreach ($attributes as $attributeInfo) { | ||
if ($attributeInfo->getIsRequired() | ||
&& (!isset($customerData[$attributeInfo->getAttributeCode()]) | ||
|| $customerData[$attributeInfo->getAttributeCode()] == '') | ||
) { | ||
$errorInput[] = $attributeInfo->getDefaultFrontendLabel(); | ||
} | ||
} | ||
|
||
if ($errorInput) { | ||
throw new GraphQlInputException( | ||
__('Required parameters are missing: %1', [implode(', ', $errorInput)]) | ||
); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.