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

Commit f763dd2

Browse files
authored
ENGCOM-4077: Create customer account functionality #254
2 parents 8df5651 + 71304f3 commit f763dd2

File tree

5 files changed

+452
-9
lines changed

5 files changed

+452
-9
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Magento\CustomerGraphQl\Model\Customer;
4+
5+
use Magento\Customer\Api\AccountManagementInterface;
6+
use Magento\Customer\Api\Data\CustomerInterface;
7+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
8+
use Magento\Framework\Api\DataObjectHelper;
9+
use Magento\Framework\Exception\LocalizedException;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
13+
/**
14+
* Class CreateAccount creates new customer account
15+
*/
16+
class CreateAccount
17+
{
18+
/**
19+
* @var DataObjectHelper
20+
*/
21+
private $dataObjectHelper;
22+
23+
/**
24+
* @var CustomerInterfaceFactory
25+
*/
26+
private $customerFactory;
27+
28+
/**
29+
* @var AccountManagementInterface
30+
*/
31+
private $accountManagement;
32+
33+
/**
34+
* @var StoreManagerInterface
35+
*/
36+
private $storeManager;
37+
38+
/**
39+
* @param DataObjectHelper $dataObjectHelper
40+
* @param CustomerInterfaceFactory $customerFactory
41+
* @param StoreManagerInterface $storeManager
42+
* @param AccountManagementInterface $accountManagement
43+
*/
44+
public function __construct(
45+
DataObjectHelper $dataObjectHelper,
46+
CustomerInterfaceFactory $customerFactory,
47+
StoreManagerInterface $storeManager,
48+
AccountManagementInterface $accountManagement
49+
) {
50+
$this->dataObjectHelper = $dataObjectHelper;
51+
$this->customerFactory = $customerFactory;
52+
$this->accountManagement = $accountManagement;
53+
$this->storeManager = $storeManager;
54+
}
55+
56+
/**
57+
* @param array $args
58+
* @return CustomerInterface
59+
* @throws LocalizedException
60+
* @throws NoSuchEntityException
61+
*/
62+
public function execute($args)
63+
{
64+
$customerDataObject = $this->customerFactory->create();
65+
$this->dataObjectHelper->populateWithArray(
66+
$customerDataObject,
67+
$args['input'],
68+
CustomerInterface::class
69+
);
70+
$store = $this->storeManager->getStore();
71+
$customerDataObject->setWebsiteId($store->getWebsiteId());
72+
$customerDataObject->setStoreId($store->getId());
73+
74+
$password = array_key_exists('password', $args['input']) ? $args['input']['password'] : null;
75+
76+
return $this->accountManagement->createAccount($customerDataObject, $password);
77+
}
78+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Magento\CustomerGraphQl\Model\Customer;
4+
5+
use Magento\Customer\Api\Data\CustomerInterface;
6+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
7+
use Magento\Authorization\Model\UserContextInterface;
8+
9+
/**
10+
* Set up user context after creating new customer account
11+
*/
12+
class SetUpUserContext
13+
{
14+
/**
15+
* @param ContextInterface $context
16+
* @param CustomerInterface $customer
17+
*/
18+
public function execute(ContextInterface $context, CustomerInterface $customer)
19+
{
20+
$context->setUserId((int)$customer->getId());
21+
$context->setUserType(UserContextInterface::USER_TYPE_CUSTOMER);
22+
}
23+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Resolver;
9+
10+
use Magento\CustomerGraphQl\Model\Customer\ChangeSubscriptionStatus;
11+
use Magento\CustomerGraphQl\Model\Customer\CreateAccount;
12+
use Magento\CustomerGraphQl\Model\Customer\CustomerDataProvider;
13+
use Magento\CustomerGraphQl\Model\Customer\SetUpUserContext;
14+
use Magento\Framework\Exception\State\InputMismatchException;
15+
use Magento\Framework\GraphQl\Config\Element\Field;
16+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
17+
use Magento\Framework\GraphQl\Query\ResolverInterface;
18+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
19+
use Magento\Framework\Validator\Exception as ValidatorException;
20+
21+
/**
22+
* Create customer account resolver
23+
*/
24+
class CreateCustomer implements ResolverInterface
25+
{
26+
/**
27+
* @var CustomerDataProvider
28+
*/
29+
private $customerDataProvider;
30+
31+
/**
32+
* @var ChangeSubscriptionStatus
33+
*/
34+
private $changeSubscriptionStatus;
35+
36+
/**
37+
* @var CreateAccount
38+
*/
39+
private $createAccount;
40+
41+
/**
42+
* @var SetUpUserContext
43+
*/
44+
private $setUpUserContext;
45+
46+
/**
47+
* @param CustomerDataProvider $customerDataProvider
48+
* @param ChangeSubscriptionStatus $changeSubscriptionStatus
49+
* @param SetUpUserContext $setUpUserContext
50+
* @param CreateAccount $createAccount
51+
*/
52+
public function __construct(
53+
CustomerDataProvider $customerDataProvider,
54+
ChangeSubscriptionStatus $changeSubscriptionStatus,
55+
SetUpUserContext $setUpUserContext,
56+
CreateAccount $createAccount
57+
) {
58+
$this->customerDataProvider = $customerDataProvider;
59+
$this->changeSubscriptionStatus = $changeSubscriptionStatus;
60+
$this->createAccount = $createAccount;
61+
$this->setUpUserContext = $setUpUserContext;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
67+
public function resolve(
68+
Field $field,
69+
$context,
70+
ResolveInfo $info,
71+
array $value = null,
72+
array $args = null
73+
) {
74+
if (!isset($args['input']) || !is_array($args['input']) || empty($args['input'])) {
75+
throw new GraphQlInputException(__('"input" value should be specified'));
76+
}
77+
try {
78+
$customer = $this->createAccount->execute($args);
79+
$customerId = (int)$customer->getId();
80+
$this->setUpUserContext->execute($context, $customer);
81+
if (array_key_exists('is_subscribed', $args['input'])) {
82+
if ($args['input']['is_subscribed']) {
83+
$this->changeSubscriptionStatus->execute($customerId, true);
84+
}
85+
}
86+
$data = $this->customerDataProvider->getCustomerById($customerId);
87+
} catch (ValidatorException $e) {
88+
throw new GraphQlInputException(__($e->getMessage()));
89+
} catch (InputMismatchException $e) {
90+
throw new GraphQlInputException(__($e->getMessage()));
91+
}
92+
93+
return ['customer' => $data];
94+
}
95+
}

app/code/Magento/CustomerGraphQl/etc/schema.graphqls

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ type Query {
88
type Mutation {
99
generateCustomerToken(email: String!, password: String!): CustomerToken @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\GenerateCustomerToken") @doc(description:"Retrieve the customer token")
1010
changeCustomerPassword(currentPassword: String!, newPassword: String!): Customer @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\ChangePassword") @doc(description:"Changes the password for the logged-in customer")
11-
updateCustomer (input: UpdateCustomerInput!): UpdateCustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Update the customer's personal information")
11+
createCustomer (input: CustomerInput!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\CreateCustomer") @doc(description:"Create customer account")
12+
updateCustomer (input: CustomerInput!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Update the customer's personal information")
1213
revokeCustomerToken: RevokeCustomerTokenOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\RevokeCustomerToken") @doc(description:"Revoke the customer token")
1314
createCustomerAddress(input: CustomerAddressInput!): CustomerAddress @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\CreateCustomerAddress") @doc(description: "Create customer address")
1415
updateCustomerAddress(id: Int!, input: CustomerAddressInput): CustomerAddress @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomerAddress") @doc(description: "Update customer address")
@@ -50,15 +51,21 @@ type CustomerToken {
5051
token: String @doc(description: "The customer token")
5152
}
5253

53-
input UpdateCustomerInput {
54-
firstname: String
55-
lastname: String
56-
email: String
57-
password: String
58-
is_subscribed: Boolean
54+
input CustomerInput {
55+
prefix: String @doc(description: "An honorific, such as Dr., Mr., or Mrs.")
56+
firstname: String @doc(description: "The customer's first name")
57+
middlename: String @doc(description: "The customer's middle name")
58+
lastname: String @doc(description: "The customer's family name")
59+
suffix: String @doc(description: "A value such as Sr., Jr., or III")
60+
email: String @doc(description: "The customer's email address. Required")
61+
dob: String @doc(description: "The customer's date of birth")
62+
taxvat: String @doc(description: "The customer's Tax/VAT number (for corporate customers)")
63+
gender: Int @doc(description: "The customer's gender(Male - 1, Female - 2)")
64+
password: String @doc(description: "The customer's password")
65+
is_subscribed: Boolean @doc(description: "Indicates whether the customer is subscribed to the company's newsletter")
5966
}
6067

61-
type UpdateCustomerOutput {
68+
type CustomerOutput {
6269
customer: Customer!
6370
}
6471

@@ -365,4 +372,4 @@ enum CountryCodeEnum @doc(description: "The list of countries codes") {
365372
YE @doc(description: "Yemen")
366373
ZM @doc(description: "Zambia")
367374
ZW @doc(description: "Zimbabwe")
368-
}
375+
}

0 commit comments

Comments
 (0)