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
  • Loading branch information
furseyev committed Mar 16, 2019
1 parent 7b1cd79 commit dba9800
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Eav\Model\Config;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;

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

/**
* @param Config $eavConfig
*/
public function __construct(Config $eavConfig)
{
$this->eavConfig = $eavConfig;
}

/**
* Get allowed address attributes
*
* @return AbstractAttribute[]
*/
public function execute(): array
{
$attributes = $this->eavConfig->getEntityAttributes(
CustomerMetadataManagementInterface::ENTITY_TYPE_CUSTOMER
);
foreach ($attributes as $attributeCode => $attribute) {
if (false === $attribute->getIsVisibleOnFront()) {
unset($attributes[$attributeCode]);
}
}
return $attributes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class UpdateCustomerAccount
*/
private $changeSubscriptionStatus;

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

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

/**
Expand All @@ -93,7 +101,7 @@ public function execute(CustomerInterface $customer, array $data): void
$this->checkCustomerPassword->execute($data['password'], (int)$customer->getId());
$customer->setEmail($data['email']);
}

$this->validateData($data);
$filteredData = array_diff_key($data, array_flip($this->restrictedKeys));
$this->dataObjectHelper->populateWithArray($customer, $filteredData, CustomerInterface::class);

Expand All @@ -105,4 +113,29 @@ 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)])
);
}
}
}

0 comments on commit dba9800

Please sign in to comment.