7
7
8
8
namespace Magento \CustomerGraphQl \Model \Resolver ;
9
9
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 ;
14
10
use Magento \CustomerGraphQl \Model \Customer \ChangeSubscriptionStatus ;
11
+ use Magento \CustomerGraphQl \Model \Customer \CreateAccount ;
15
12
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 ;
19
14
use Magento \Framework \Exception \State \InputMismatchException ;
20
15
use Magento \Framework \GraphQl \Config \Element \Field ;
21
16
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
22
- use Magento \Framework \GraphQl \Query \Resolver \ContextInterface ;
23
- use Magento \Framework \GraphQl \Query \Resolver \Value ;
24
17
use Magento \Framework \GraphQl \Query \ResolverInterface ;
25
18
use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
26
19
use Magento \Framework \Validator \Exception as ValidatorException ;
27
- use Magento \Newsletter \Model \SubscriberFactory ;
28
- use Magento \Store \Model \StoreManagerInterface ;
29
20
21
+ /**
22
+ * Create customer account resolver
23
+ */
30
24
class CreateCustomer implements ResolverInterface
31
25
{
32
26
/**
33
27
* @var CustomerDataProvider
34
28
*/
35
29
private $ customerDataProvider ;
30
+
36
31
/**
37
- * @var AccountManagementInterface
38
- */
39
- private $ accountManagement ;
40
- /**
41
- * @var CustomerInterfaceFactory
42
- */
43
- private $ customerFactory ;
44
- /**
45
- * @var DataObjectHelper
32
+ * @var ChangeSubscriptionStatus
46
33
*/
47
- private $ dataObjectHelper ;
34
+ private $ changeSubscriptionStatus ;
35
+
48
36
/**
49
- * @var StoreManagerInterface
37
+ * @var CreateAccount
50
38
*/
51
- private $ storeManager ;
39
+ private $ createAccount ;
40
+
52
41
/**
53
- * @var SubscriberFactory
42
+ * @var SetUpUserContext
54
43
*/
55
- private $ subscriberFactory ;
56
- /**
57
- * @var ChangeSubscriptionStatus
58
- */
59
- private $ changeSubscriptionStatus ;
44
+ private $ setUpUserContext ;
60
45
61
46
/**
62
- * @param DataObjectHelper $dataObjectHelper
63
- * @param CustomerInterfaceFactory $customerFactory
64
- * @param AccountManagementInterface $accountManagement
65
- * @param StoreManagerInterface $storeManager
66
- * @param SubscriberFactory $subscriberFactory
67
47
* @param CustomerDataProvider $customerDataProvider
68
48
* @param ChangeSubscriptionStatus $changeSubscriptionStatus
49
+ * @param SetUpUserContext $setUpUserContext
50
+ * @param CreateAccount $createAccount
69
51
*/
70
52
public function __construct (
71
- DataObjectHelper $ dataObjectHelper ,
72
- CustomerInterfaceFactory $ customerFactory ,
73
- AccountManagementInterface $ accountManagement ,
74
- StoreManagerInterface $ storeManager ,
75
- SubscriberFactory $ subscriberFactory ,
76
53
CustomerDataProvider $ customerDataProvider ,
77
- ChangeSubscriptionStatus $ changeSubscriptionStatus
54
+ ChangeSubscriptionStatus $ changeSubscriptionStatus ,
55
+ SetUpUserContext $ setUpUserContext ,
56
+ CreateAccount $ createAccount
78
57
) {
79
58
$ this ->customerDataProvider = $ customerDataProvider ;
80
- $ this ->accountManagement = $ accountManagement ;
81
- $ this ->customerFactory = $ customerFactory ;
82
- $ this ->dataObjectHelper = $ dataObjectHelper ;
83
- $ this ->storeManager = $ storeManager ;
84
- $ this ->subscriberFactory = $ subscriberFactory ;
85
59
$ this ->changeSubscriptionStatus = $ changeSubscriptionStatus ;
60
+ $ this ->createAccount = $ createAccount ;
61
+ $ this ->setUpUserContext = $ setUpUserContext ;
86
62
}
87
63
88
64
/**
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
98
66
*/
99
67
public function resolve (
100
68
Field $ field ,
@@ -107,9 +75,9 @@ public function resolve(
107
75
throw new GraphQlInputException (__ ('"input" value should be specified ' ));
108
76
}
109
77
try {
110
- $ customer = $ this ->createUserAccount ($ args );
78
+ $ customer = $ this ->createAccount -> execute ($ args );
111
79
$ customerId = (int )$ customer ->getId ();
112
- $ this ->setUpUserContext ($ context , $ customer );
80
+ $ this ->setUpUserContext -> execute ($ context , $ customer );
113
81
if (array_key_exists ('is_subscribed ' , $ args ['input ' ])) {
114
82
if ($ args ['input ' ]['is_subscribed ' ]) {
115
83
$ this ->changeSubscriptionStatus ->execute ($ customerId , true );
@@ -124,37 +92,4 @@ public function resolve(
124
92
125
93
return ['customer ' => $ data ];
126
94
}
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
- }
160
95
}
0 commit comments