-
Notifications
You must be signed in to change notification settings - Fork 152
Create customer account functionality #254
Changes from all commits
6d5a80c
91edcc5
3563671
af0b38b
76f9315
bcfae0c
176bc4a
9bf12cc
906aeb2
0136faf
255b8f2
f2b43e1
7c5bea3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Magento\CustomerGraphQl\Model\Customer; | ||
|
||
use Magento\Customer\Api\AccountManagementInterface; | ||
use Magento\Customer\Api\Data\CustomerInterface; | ||
use Magento\Customer\Api\Data\CustomerInterfaceFactory; | ||
use Magento\Framework\Api\DataObjectHelper; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
/** | ||
* Class CreateAccount creates new customer account | ||
*/ | ||
class CreateAccount | ||
{ | ||
/** | ||
* @var DataObjectHelper | ||
*/ | ||
private $dataObjectHelper; | ||
|
||
/** | ||
* @var CustomerInterfaceFactory | ||
*/ | ||
private $customerFactory; | ||
|
||
/** | ||
* @var AccountManagementInterface | ||
*/ | ||
private $accountManagement; | ||
|
||
/** | ||
* @var StoreManagerInterface | ||
*/ | ||
private $storeManager; | ||
|
||
/** | ||
* @param DataObjectHelper $dataObjectHelper | ||
* @param CustomerInterfaceFactory $customerFactory | ||
* @param StoreManagerInterface $storeManager | ||
* @param AccountManagementInterface $accountManagement | ||
*/ | ||
public function __construct( | ||
DataObjectHelper $dataObjectHelper, | ||
CustomerInterfaceFactory $customerFactory, | ||
StoreManagerInterface $storeManager, | ||
AccountManagementInterface $accountManagement | ||
) { | ||
$this->dataObjectHelper = $dataObjectHelper; | ||
$this->customerFactory = $customerFactory; | ||
$this->accountManagement = $accountManagement; | ||
$this->storeManager = $storeManager; | ||
} | ||
|
||
/** | ||
* @param array $args | ||
* @return CustomerInterface | ||
* @throws LocalizedException | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function execute($args) | ||
{ | ||
$customerDataObject = $this->customerFactory->create(); | ||
$this->dataObjectHelper->populateWithArray( | ||
$customerDataObject, | ||
$args['input'], | ||
CustomerInterface::class | ||
); | ||
$store = $this->storeManager->getStore(); | ||
$customerDataObject->setWebsiteId($store->getWebsiteId()); | ||
$customerDataObject->setStoreId($store->getId()); | ||
|
||
$password = array_key_exists('password', $args['input']) ? $args['input']['password'] : null; | ||
|
||
return $this->accountManagement->createAccount($customerDataObject, $password); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Magento\CustomerGraphQl\Model\Customer; | ||
|
||
use Magento\Customer\Api\Data\CustomerInterface; | ||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; | ||
use Magento\Authorization\Model\UserContextInterface; | ||
|
||
/** | ||
* Set up user context after creating new customer account | ||
*/ | ||
class SetUpUserContext | ||
{ | ||
/** | ||
* @param ContextInterface $context | ||
* @param CustomerInterface $customer | ||
*/ | ||
public function execute(ContextInterface $context, CustomerInterface $customer) | ||
{ | ||
$context->setUserId((int)$customer->getId()); | ||
$context->setUserType(UserContextInterface::USER_TYPE_CUSTOMER); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Resolver; | ||
|
||
use Magento\CustomerGraphQl\Model\Customer\ChangeSubscriptionStatus; | ||
use Magento\CustomerGraphQl\Model\Customer\CreateAccount; | ||
use Magento\CustomerGraphQl\Model\Customer\CustomerDataProvider; | ||
use Magento\CustomerGraphQl\Model\Customer\SetUpUserContext; | ||
use Magento\Framework\Exception\State\InputMismatchException; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\Validator\Exception as ValidatorException; | ||
|
||
/** | ||
* Create customer account resolver | ||
*/ | ||
class CreateCustomer implements ResolverInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to create |
||
{ | ||
/** | ||
* @var CustomerDataProvider | ||
*/ | ||
private $customerDataProvider; | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missed rmpty line |
||
* @var ChangeSubscriptionStatus | ||
*/ | ||
private $changeSubscriptionStatus; | ||
|
||
/** | ||
* @var CreateAccount | ||
*/ | ||
private $createAccount; | ||
|
||
/** | ||
* @var SetUpUserContext | ||
*/ | ||
private $setUpUserContext; | ||
|
||
/** | ||
* @param CustomerDataProvider $customerDataProvider | ||
* @param ChangeSubscriptionStatus $changeSubscriptionStatus | ||
* @param SetUpUserContext $setUpUserContext | ||
* @param CreateAccount $createAccount | ||
*/ | ||
public function __construct( | ||
CustomerDataProvider $customerDataProvider, | ||
ChangeSubscriptionStatus $changeSubscriptionStatus, | ||
SetUpUserContext $setUpUserContext, | ||
CreateAccount $createAccount | ||
) { | ||
$this->customerDataProvider = $customerDataProvider; | ||
$this->changeSubscriptionStatus = $changeSubscriptionStatus; | ||
$this->createAccount = $createAccount; | ||
$this->setUpUserContext = $setUpUserContext; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use |
||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) { | ||
if (!isset($args['input']) || !is_array($args['input']) || empty($args['input'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to have more smarty validation (based on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got it from the current \Magento\CustomerGraphQl\Model\Resolver\UpdateCustomer implementation. Is it ok for updating but not ok for creation? Just question to think. We can have attribute requirement changed between account creation and account updating. So it also needs to be validated in customer update. My implementation checks which fields are required There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
throw new GraphQlInputException(__('"input" value should be specified')); | ||
} | ||
try { | ||
$customer = $this->createAccount->execute($args); | ||
$customerId = (int)$customer->getId(); | ||
$this->setUpUserContext->execute($context, $customer); | ||
if (array_key_exists('is_subscribed', $args['input'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pls, reuse There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
if ($args['input']['is_subscribed']) { | ||
$this->changeSubscriptionStatus->execute($customerId, true); | ||
} | ||
} | ||
$data = $this->customerDataProvider->getCustomerById($customerId); | ||
} catch (ValidatorException $e) { | ||
throw new GraphQlInputException(__($e->getMessage())); | ||
} catch (InputMismatchException $e) { | ||
throw new GraphQlInputException(__($e->getMessage())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to highlight to client all of possible errors There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
} | ||
|
||
return ['customer' => $data]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to split this class (an example is in other resolvers of this module)