Skip to content

Commit

Permalink
magento#271: [My Account] Refactoring the addition of required attrib…
Browse files Browse the repository at this point in the history
…utes to validation of customer creation
  • Loading branch information
furseyev committed Jul 18, 2019
1 parent 4382c3a commit da8d6ac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\Reflection\DataObjectProcessor;
use Magento\Store\Model\StoreManagerInterface;

/**
Expand Down Expand Up @@ -50,6 +51,11 @@ class CreateCustomerAccount
*/
private $validateCustomerData;

/**
* @var DataObjectProcessor
*/
private $dataObjectProcessor;

/**
* CreateCustomerAccount constructor.
*
Expand All @@ -58,6 +64,7 @@ class CreateCustomerAccount
* @param StoreManagerInterface $storeManager
* @param AccountManagementInterface $accountManagement
* @param ChangeSubscriptionStatus $changeSubscriptionStatus
* @param DataObjectProcessor $dataObjectProcessor
* @param ValidateCustomerData $validateCustomerData
*/
public function __construct(
Expand All @@ -66,6 +73,7 @@ public function __construct(
StoreManagerInterface $storeManager,
AccountManagementInterface $accountManagement,
ChangeSubscriptionStatus $changeSubscriptionStatus,
DataObjectProcessor $dataObjectProcessor,
ValidateCustomerData $validateCustomerData
) {
$this->dataObjectHelper = $dataObjectHelper;
Expand All @@ -74,6 +82,7 @@ public function __construct(
$this->storeManager = $storeManager;
$this->changeSubscriptionStatus = $changeSubscriptionStatus;
$this->validateCustomerData = $validateCustomerData;
$this->dataObjectProcessor = $dataObjectProcessor;
}

/**
Expand Down Expand Up @@ -106,8 +115,16 @@ public function execute(array $data): CustomerInterface
*/
private function createAccount(array $data): CustomerInterface
{
$this->validateCustomerData->execute($data, true);
$customerDataObject = $this->customerFactory->create();
/**
* Add required attributes for customer entity
*/
$requiredDataAttributes = $this->dataObjectProcessor->buildOutputDataArray(
$customerDataObject,
CustomerInterface::class
);
$data = array_merge($requiredDataAttributes, $data);
$this->validateCustomerData->execute($data);
$this->dataObjectHelper->populateWithArray(
$customerDataObject,
$data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,13 @@ public function __construct(
* Get allowed customer attributes
*
* @param array $attributeKeys
* @param bool $addRequiredAttributes
*
* @throws GraphQlInputException
*
* @return AbstractAttribute[]
*/
public function execute($attributeKeys, $addRequiredAttributes = false): array
public function execute($attributeKeys): array
{
/**
* Add required attributes for customer entity to passed attribute keys
*/
if ($addRequiredAttributes) {
/** @var CustomerInterface $customerDataDummy */
$customerDataDummy = $this->customerDataFactory->create();
$requiredDataAttributes = $this->dataObjectProcessor->buildOutputDataArray(
$customerDataDummy,
CustomerInterface::class
);
$attributeKeys = array_merge($attributeKeys, array_keys($requiredDataAttributes));
}

$this->searchCriteriaBuilder->addFilter('attribute_code', $attributeKeys, 'in');
$searchCriteria = $this->searchCriteriaBuilder->create();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ public function __construct(GetAllowedCustomerAttributes $getAllowedCustomerAttr
* Validate customer data
*
* @param array $customerData
* @param bool $addRequiredAttributes
*
* @return void
*
* @throws GraphQlInputException
*/
public function execute(array $customerData, $addRequiredAttributes = false): void
public function execute(array $customerData): void
{
$attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData), $addRequiredAttributes);
$attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData));
$errorInput = [];

foreach ($attributes as $attributeInfo) {
Expand Down

0 comments on commit da8d6ac

Please sign in to comment.