13
13
use Magento \Framework \Serialize \SerializerInterface ;
14
14
use Magento \Framework \Webapi \ServiceOutputProcessor ;
15
15
use Magento \Customer \Api \Data \CustomerInterface ;
16
+ use Magento \Newsletter \Model \SubscriberFactory ;
17
+ use Magento \Customer \Model \CustomerRegistry ;
18
+ use Magento \Framework \Encryption \EncryptorInterface as Encryptor ;
19
+ use Magento \Store \Api \StoreResolverInterface ;
20
+ use Magento \Framework \GraphQl \Exception \GraphQlAuthorizationException ;
16
21
17
22
/**
18
23
* Customer field data provider, used for GraphQL request processing.
@@ -29,11 +34,31 @@ class CustomerDataProvider
29
34
*/
30
35
private $ serviceOutputProcessor ;
31
36
37
+ /**
38
+ * @var StoreResolverInterface
39
+ */
40
+ private $ storeResolver ;
41
+
42
+ /**
43
+ * @var \Magento\Newsletter\Model\SubscriberFactory
44
+ */
45
+ protected $ subscriberFactory ;
46
+
47
+ /**
48
+ * @var CustomerRegistry
49
+ */
50
+ protected $ customerRegistry ;
51
+
32
52
/**
33
53
* @var SerializerInterface
34
54
*/
35
55
private $ jsonSerializer ;
36
56
57
+ /**
58
+ * @var Encryptor
59
+ */
60
+ protected $ encryptor ;
61
+
37
62
/**
38
63
* @param CustomerRepositoryInterface $customerRepository
39
64
* @param ServiceOutputProcessor $serviceOutputProcessor
@@ -42,11 +67,32 @@ class CustomerDataProvider
42
67
public function __construct (
43
68
CustomerRepositoryInterface $ customerRepository ,
44
69
ServiceOutputProcessor $ serviceOutputProcessor ,
45
- SerializerInterface $ jsonSerializer
70
+ SerializerInterface $ jsonSerializer ,
71
+ SubscriberFactory $ subscriberFactory ,
72
+ CustomerRegistry $ customerRegistry ,
73
+ Encryptor $ encryptor ,
74
+ StoreResolverInterface $ storeResolver
46
75
) {
47
76
$ this ->customerRepository = $ customerRepository ;
48
77
$ this ->serviceOutputProcessor = $ serviceOutputProcessor ;
49
78
$ this ->jsonSerializer = $ jsonSerializer ;
79
+ $ this ->subscriberFactory = $ subscriberFactory ;
80
+ $ this ->customerRegistry = $ customerRegistry ;
81
+ $ this ->encryptor = $ encryptor ;
82
+ $ this ->storeResolver = $ storeResolver ;
83
+ }
84
+
85
+ /**
86
+ * Load customer object
87
+ *
88
+ * @param int $customerId
89
+ * @return CustomerInterface
90
+ * @throws LocalizedException
91
+ * @throws NoSuchEntityException
92
+ */
93
+ public function loadCustomerById (int $ customerId ): CustomerInterface
94
+ {
95
+ return $ this ->customerRepository ->getById ($ customerId );
50
96
}
51
97
52
98
/**
@@ -56,7 +102,7 @@ public function __construct(
56
102
* @return array
57
103
* @throws NoSuchEntityException|LocalizedException
58
104
*/
59
- public function getCustomerById (int $ customerId ) : array
105
+ public function getCustomerById (int $ customerId ): array
60
106
{
61
107
try {
62
108
$ customerObject = $ this ->customerRepository ->getById ($ customerId );
@@ -73,7 +119,7 @@ public function getCustomerById(int $customerId) : array
73
119
* @param CustomerInterface $customerObject
74
120
* @return array
75
121
*/
76
- private function processCustomer (CustomerInterface $ customerObject ) : array
122
+ private function processCustomer (CustomerInterface $ customerObject ): array
77
123
{
78
124
$ customer = $ this ->serviceOutputProcessor ->process (
79
125
$ customerObject ,
@@ -110,4 +156,84 @@ private function processCustomer(CustomerInterface $customerObject) : array
110
156
111
157
return $ customer ;
112
158
}
159
+
160
+ /**
161
+ * Check if customer is subscribed to Newsletter
162
+ *
163
+ * @param int $customerId
164
+ * @return bool
165
+ */
166
+ public function isSubscribed (int $ customerId ): bool
167
+ {
168
+ $ checkSubscriber = $ this ->subscriberFactory ->create ()->loadByCustomerId ($ customerId );
169
+ return $ checkSubscriber ->isSubscribed ();
170
+ }
171
+
172
+ /**
173
+ * Manage customer subscription. Subscribe OR unsubscribe if required
174
+ *
175
+ * @param int $customerId
176
+ * @param $newSubscriptionStatus
177
+ * @return bool
178
+ */
179
+ public function manageSubscription (int $ customerId , bool $ newSubscriptionStatus ): bool
180
+ {
181
+ $ checkSubscriber = $ this ->subscriberFactory ->create ()->loadByCustomerId ($ customerId );
182
+ $ isSubscribed = $ this ->isSubscribed ($ customerId );
183
+
184
+ if ($ newSubscriptionStatus === true && !$ isSubscribed ) {
185
+ $ this ->subscriberFactory ->create ()->subscribeCustomerById ($ customerId );
186
+ } elseif ($ newSubscriptionStatus === false && $ checkSubscriber ->isSubscribed ()) {
187
+ $ this ->subscriberFactory ->create ()->unsubscribeCustomerById ($ customerId );
188
+ }
189
+ return true ;
190
+ }
191
+
192
+ /**
193
+ * @param int $customerId
194
+ * @param array $customerData
195
+ * @return CustomerInterface
196
+ * @throws LocalizedException
197
+ * @throws NoSuchEntityException
198
+ * @throws \Magento\Framework\Exception\InputException
199
+ * @throws \Magento\Framework\Exception\State\InputMismatchException
200
+ */
201
+ public function updateAccountInformation (int $ customerId , array $ customerData ): CustomerInterface
202
+ {
203
+
204
+ $ customer = $ this ->loadCustomerById ($ customerId );
205
+
206
+ if (isset ($ customerData ['email ' ])
207
+ && $ customer ->getEmail () !== $ customerData ['email ' ]
208
+ && isset ($ customerData ['password ' ])) {
209
+ if ($ this ->isPasswordCorrect ($ customerData ['password ' ], $ customerId )) {
210
+ $ customer ->setEmail ($ customerData ['email ' ]);
211
+ } else {
212
+ throw new GraphQlAuthorizationException (__ ('Invalid current user password. ' ));
213
+ }
214
+ }
215
+
216
+ if (isset ($ customerData ['firstname ' ])) {
217
+ $ customer ->setFirstname ($ customerData ['firstname ' ]);
218
+ }
219
+ if (isset ($ customerData ['lastname ' ])) {
220
+ $ customer ->setLastname ($ customerData ['lastname ' ]);
221
+ }
222
+
223
+ $ customer ->setStoreId ($ this ->storeResolver ->getCurrentStoreId ());
224
+ $ this ->customerRepository ->save ($ customer );
225
+
226
+ return $ customer ;
227
+ }
228
+
229
+ private function isPasswordCorrect (string $ password , int $ customerId )
230
+ {
231
+
232
+ $ customerSecure = $ this ->customerRegistry ->retrieveSecureData ($ customerId );
233
+ $ hash = $ customerSecure ->getPasswordHash ();
234
+ if (!$ this ->encryptor ->validateHash ($ password , $ hash )) {
235
+ return false ;
236
+ }
237
+ return true ;
238
+ }
113
239
}
0 commit comments