Skip to content

Commit

Permalink
magento/graphql-ce#271: [My Account] Add support of Customer attributes
Browse files Browse the repository at this point in the history
- Customer attributes validation added for create customer action
  • Loading branch information
furseyev committed Mar 16, 2019
1 parent 7f6718d commit 3735e91
Showing 1 changed file with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,33 @@ class CreateCustomerAccount
*/
private $changeSubscriptionStatus;

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

/**
* @param DataObjectHelper $dataObjectHelper
* @param CustomerInterfaceFactory $customerFactory
* @param StoreManagerInterface $storeManager
* @param AccountManagementInterface $accountManagement
* @param ChangeSubscriptionStatus $changeSubscriptionStatus
* @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes
*/
public function __construct(
DataObjectHelper $dataObjectHelper,
CustomerInterfaceFactory $customerFactory,
StoreManagerInterface $storeManager,
AccountManagementInterface $accountManagement,
ChangeSubscriptionStatus $changeSubscriptionStatus
ChangeSubscriptionStatus $changeSubscriptionStatus,
GetAllowedCustomerAttributes $getAllowedCustomerAttributes
) {
$this->dataObjectHelper = $dataObjectHelper;
$this->customerFactory = $customerFactory;
$this->accountManagement = $accountManagement;
$this->storeManager = $storeManager;
$this->changeSubscriptionStatus = $changeSubscriptionStatus;
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes;
}

/**
Expand Down Expand Up @@ -96,6 +104,7 @@ public function execute(array $data): CustomerInterface
*/
private function createAccount(array $data): CustomerInterface
{
$this->validateData($data);
$customerDataObject = $this->customerFactory->create();
$this->dataObjectHelper->populateWithArray(
$customerDataObject,
Expand All @@ -109,4 +118,29 @@ 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)])
);
}
}
}

0 comments on commit 3735e91

Please sign in to comment.