Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
#271: [My Account] Refactoring customer attributes validation
Browse files Browse the repository at this point in the history
  • Loading branch information
furseyev committed May 6, 2019
1 parent 9b5b0a1 commit 9f1ba1a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class CreateCustomerAccount
private $changeSubscriptionStatus;

/**
* @var GetAllowedCustomerAttributes
* @var ValidateCustomerData
*/
private $getAllowedCustomerAttributes;
private $validateCustomerData;

/**
* @param DataObjectHelper $dataObjectHelper
Expand All @@ -64,14 +64,14 @@ public function __construct(
StoreManagerInterface $storeManager,
AccountManagementInterface $accountManagement,
ChangeSubscriptionStatus $changeSubscriptionStatus,
GetAllowedCustomerAttributes $getAllowedCustomerAttributes
ValidateCustomerData $validateCustomerData
) {
$this->dataObjectHelper = $dataObjectHelper;
$this->customerFactory = $customerFactory;
$this->accountManagement = $accountManagement;
$this->storeManager = $storeManager;
$this->changeSubscriptionStatus = $changeSubscriptionStatus;
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes;
$this->validateCustomerData = $validateCustomerData;
}

/**
Expand Down Expand Up @@ -104,7 +104,7 @@ public function execute(array $data): CustomerInterface
*/
private function createAccount(array $data): CustomerInterface
{
$this->validateData($data);
$this->validateCustomerData->execute($data);
$customerDataObject = $this->customerFactory->create();
$this->dataObjectHelper->populateWithArray(
$customerDataObject,
Expand All @@ -118,29 +118,4 @@ private function createAccount(array $data): CustomerInterface
$password = array_key_exists('password', $data) ? $data['password'] : null;
return $this->accountManagement->createAccount($customerDataObject, $password);
}

/**
* @param array $customerData
* @return void
* @throws GraphQlInputException
*/
public function validateData(array $customerData): void
{
$attributes = $this->getAllowedCustomerAttributes->execute();
$errorInput = [];

foreach ($attributes as $attributeName => $attributeInfo) {
if ($attributeInfo->getIsRequired()
&& (isset($customerData[$attributeName]) && empty($customerData[$attributeName]))
) {
$errorInput[] = $attributeName;
}
}

if ($errorInput) {
throw new GraphQlInputException(
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,67 @@
namespace Magento\CustomerGraphQl\Model\Customer;

use Magento\Customer\Api\CustomerMetadataManagementInterface;
use Magento\Eav\Model\Config;
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;

/**
* Get allowed address attributes
*/
class GetAllowedCustomerAttributes
{
/**
* @var Config
* @var AttributeRepository
*/
private $eavConfig;
private $attributeRepository;

/**
* @param Config $eavConfig
* @var SearchCriteriaBuilder
*/
public function __construct(Config $eavConfig)
{
$this->eavConfig = $eavConfig;
private $searchCriteriaBuilder;

/**
* @param AttributeRepository $attributeRepository
*/
public function __construct(
AttributeRepository $attributeRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->attributeRepository = $attributeRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
* Get allowed address attributes
* Get allowed customer attributes
*
* @param array $attributeKeys
* @throws GraphQlInputException
* @return AbstractAttribute[]
*/
public function execute(): array
public function execute($attributeKeys): array
{
$attributes = $this->eavConfig->getEntityAttributes(
CustomerMetadataManagementInterface::ENTITY_TYPE_CUSTOMER
);
foreach ($attributes as $attributeCode => $attribute) {
$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[$attributeCode]);
unset($attributes[$index]);
}
}

return $attributes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class UpdateCustomerAccount
private $changeSubscriptionStatus;

/**
* @var GetAllowedCustomerAttributes
* @var ValidateCustomerData
*/
private $getAllowedCustomerAttributes;
private $validateCustomerData;

/**
* @var array
Expand All @@ -60,7 +60,7 @@ class UpdateCustomerAccount
* @param CheckCustomerPassword $checkCustomerPassword
* @param DataObjectHelper $dataObjectHelper
* @param ChangeSubscriptionStatus $changeSubscriptionStatus
* @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes
* @param ValidateCustomerData $validateCustomerData
* @param array $restrictedKeys
*/
public function __construct(
Expand All @@ -69,7 +69,7 @@ public function __construct(
CheckCustomerPassword $checkCustomerPassword,
DataObjectHelper $dataObjectHelper,
ChangeSubscriptionStatus $changeSubscriptionStatus,
GetAllowedCustomerAttributes $getAllowedCustomerAttributes,
ValidateCustomerData $validateCustomerData,
array $restrictedKeys = []
) {
$this->saveCustomer = $saveCustomer;
Expand All @@ -78,18 +78,17 @@ public function __construct(
$this->dataObjectHelper = $dataObjectHelper;
$this->restrictedKeys = $restrictedKeys;
$this->changeSubscriptionStatus = $changeSubscriptionStatus;
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes;
$this->validateCustomerData = $validateCustomerData;
}

/**
* Update customer account data
*
* @param CustomerInterface $customer
* @param array $data
* @return void
* @throws GraphQlAlreadyExistsException
* @throws GraphQlAuthenticationException
* @throws GraphQlInputException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException
*/
public function execute(CustomerInterface $customer, array $data): void
{
Expand All @@ -101,7 +100,7 @@ public function execute(CustomerInterface $customer, array $data): void
$this->checkCustomerPassword->execute($data['password'], (int)$customer->getId());
$customer->setEmail($data['email']);
}
$this->validateData($data);
$this->validateCustomerData->execute($data);
$filteredData = array_diff_key($data, array_flip($this->restrictedKeys));
$this->dataObjectHelper->populateWithArray($customer, $filteredData, CustomerInterface::class);

Expand All @@ -113,29 +112,4 @@ public function execute(CustomerInterface $customer, array $data): void
$this->changeSubscriptionStatus->execute((int)$customer->getId(), (bool)$data['is_subscribed']);
}
}

/**
* @param array $customerData
* @return void
* @throws GraphQlInputException
*/
public function validateData(array $customerData): void
{
$attributes = $this->getAllowedCustomerAttributes->execute();
$errorInput = [];

foreach ($attributes as $attributeName => $attributeInfo) {
if ($attributeInfo->getIsRequired()
&& (isset($customerData[$attributeName]) && empty($customerData[$attributeName]))
) {
$errorInput[] = $attributeName;
}
}

if ($errorInput) {
throw new GraphQlInputException(
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
);
}
}
}
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)])
);
}
}
}

0 comments on commit 9f1ba1a

Please sign in to comment.