Skip to content

Commit 255b8f2

Browse files
committed
split class, format changes
1 parent 0136faf commit 255b8f2

File tree

3 files changed

+125
-89
lines changed

3 files changed

+125
-89
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+
}

app/code/Magento/CustomerGraphQl/Model/Resolver/CreateCustomer.php

Lines changed: 24 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -7,94 +7,62 @@
77

88
namespace Magento\CustomerGraphQl\Model\Resolver;
99

10-
use Magento\Authorization\Model\UserContextInterface;
11-
use Magento\Customer\Api\AccountManagementInterface;
12-
use Magento\Customer\Api\Data\CustomerInterface;
13-
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
1410
use Magento\CustomerGraphQl\Model\Customer\ChangeSubscriptionStatus;
11+
use Magento\CustomerGraphQl\Model\Customer\CreateAccount;
1512
use Magento\CustomerGraphQl\Model\Customer\CustomerDataProvider;
16-
use Magento\Framework\Api\DataObjectHelper;
17-
use Magento\Framework\Exception\LocalizedException;
18-
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\CustomerGraphQl\Model\Customer\SetUpUserContext;
1914
use Magento\Framework\Exception\State\InputMismatchException;
2015
use Magento\Framework\GraphQl\Config\Element\Field;
2116
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
22-
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
23-
use Magento\Framework\GraphQl\Query\Resolver\Value;
2417
use Magento\Framework\GraphQl\Query\ResolverInterface;
2518
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
2619
use Magento\Framework\Validator\Exception as ValidatorException;
27-
use Magento\Newsletter\Model\SubscriberFactory;
28-
use Magento\Store\Model\StoreManagerInterface;
2920

21+
/**
22+
* Create customer account resolver
23+
*/
3024
class CreateCustomer implements ResolverInterface
3125
{
3226
/**
3327
* @var CustomerDataProvider
3428
*/
3529
private $customerDataProvider;
30+
3631
/**
37-
* @var AccountManagementInterface
38-
*/
39-
private $accountManagement;
40-
/**
41-
* @var CustomerInterfaceFactory
42-
*/
43-
private $customerFactory;
44-
/**
45-
* @var DataObjectHelper
32+
* @var ChangeSubscriptionStatus
4633
*/
47-
private $dataObjectHelper;
34+
private $changeSubscriptionStatus;
35+
4836
/**
49-
* @var StoreManagerInterface
37+
* @var CreateAccount
5038
*/
51-
private $storeManager;
39+
private $createAccount;
40+
5241
/**
53-
* @var SubscriberFactory
42+
* @var SetUpUserContext
5443
*/
55-
private $subscriberFactory;
56-
/**
57-
* @var ChangeSubscriptionStatus
58-
*/
59-
private $changeSubscriptionStatus;
44+
private $setUpUserContext;
6045

6146
/**
62-
* @param DataObjectHelper $dataObjectHelper
63-
* @param CustomerInterfaceFactory $customerFactory
64-
* @param AccountManagementInterface $accountManagement
65-
* @param StoreManagerInterface $storeManager
66-
* @param SubscriberFactory $subscriberFactory
6747
* @param CustomerDataProvider $customerDataProvider
6848
* @param ChangeSubscriptionStatus $changeSubscriptionStatus
49+
* @param SetUpUserContext $setUpUserContext
50+
* @param CreateAccount $createAccount
6951
*/
7052
public function __construct(
71-
DataObjectHelper $dataObjectHelper,
72-
CustomerInterfaceFactory $customerFactory,
73-
AccountManagementInterface $accountManagement,
74-
StoreManagerInterface $storeManager,
75-
SubscriberFactory $subscriberFactory,
7653
CustomerDataProvider $customerDataProvider,
77-
ChangeSubscriptionStatus $changeSubscriptionStatus
54+
ChangeSubscriptionStatus $changeSubscriptionStatus,
55+
SetUpUserContext $setUpUserContext,
56+
CreateAccount $createAccount
7857
) {
7958
$this->customerDataProvider = $customerDataProvider;
80-
$this->accountManagement = $accountManagement;
81-
$this->customerFactory = $customerFactory;
82-
$this->dataObjectHelper = $dataObjectHelper;
83-
$this->storeManager = $storeManager;
84-
$this->subscriberFactory = $subscriberFactory;
8559
$this->changeSubscriptionStatus = $changeSubscriptionStatus;
60+
$this->createAccount = $createAccount;
61+
$this->setUpUserContext = $setUpUserContext;
8662
}
8763

8864
/**
89-
* @param Field $field
90-
* @param ContextInterface $context
91-
* @param ResolveInfo $info
92-
* @param array|null $value
93-
* @param array|null $args
94-
* @return array|Value|mixed
95-
* @throws GraphQlInputException
96-
* @throws LocalizedException
97-
* @throws NoSuchEntityException
65+
* @inheritdoc
9866
*/
9967
public function resolve(
10068
Field $field,
@@ -107,9 +75,9 @@ public function resolve(
10775
throw new GraphQlInputException(__('"input" value should be specified'));
10876
}
10977
try {
110-
$customer = $this->createUserAccount($args);
78+
$customer = $this->createAccount->execute($args);
11179
$customerId = (int)$customer->getId();
112-
$this->setUpUserContext($context, $customer);
80+
$this->setUpUserContext->execute($context, $customer);
11381
if (array_key_exists('is_subscribed', $args['input'])) {
11482
if ($args['input']['is_subscribed']) {
11583
$this->changeSubscriptionStatus->execute($customerId, true);
@@ -124,37 +92,4 @@ public function resolve(
12492

12593
return ['customer' => $data];
12694
}
127-
128-
/**
129-
* @param $args
130-
* @return CustomerInterface
131-
* @throws LocalizedException
132-
* @throws NoSuchEntityException
133-
*/
134-
private function createUserAccount($args)
135-
{
136-
$customerDataObject = $this->customerFactory->create();
137-
$this->dataObjectHelper->populateWithArray(
138-
$customerDataObject,
139-
$args['input'],
140-
CustomerInterface::class
141-
);
142-
$store = $this->storeManager->getStore();
143-
$customerDataObject->setWebsiteId($store->getWebsiteId());
144-
$customerDataObject->setStoreId($store->getId());
145-
146-
$password = array_key_exists('password', $args['input']) ? $args['input']['password'] : null;
147-
148-
return $this->accountManagement->createAccount($customerDataObject, $password);
149-
}
150-
151-
/**
152-
* @param $context
153-
* @param CustomerInterface $customer
154-
*/
155-
private function setUpUserContext($context, $customer)
156-
{
157-
$context->setUserId((int)$customer->getId());
158-
$context->setUserType(UserContextInterface::USER_TYPE_CUSTOMER);
159-
}
16095
}

0 commit comments

Comments
 (0)