Skip to content

Commit d0dd90f

Browse files
committed
Refactored Account Mutation
1 parent 8963811 commit d0dd90f

File tree

5 files changed

+291
-106
lines changed

5 files changed

+291
-106
lines changed

app/code/Magento/CustomerGraphQl/Model/Resolver/Customer/CustomerDataProvider.php

+5-103
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
use Magento\Framework\Serialize\SerializerInterface;
1414
use Magento\Framework\Webapi\ServiceOutputProcessor;
1515
use Magento\Customer\Api\Data\CustomerInterface;
16-
use Magento\Newsletter\Model\SubscriberFactory;
1716
use Magento\Customer\Model\CustomerRegistry;
1817
use Magento\Framework\Encryption\EncryptorInterface as Encryptor;
19-
use Magento\Store\Api\StoreResolverInterface;
20-
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
2118

2219
/**
2320
* Customer field data provider, used for GraphQL request processing.
@@ -34,16 +31,6 @@ class CustomerDataProvider
3431
*/
3532
private $serviceOutputProcessor;
3633

37-
/**
38-
* @var StoreResolverInterface
39-
*/
40-
private $storeResolver;
41-
42-
/**
43-
* @var \Magento\Newsletter\Model\SubscriberFactory
44-
*/
45-
protected $subscriberFactory;
46-
4734
/**
4835
* @var CustomerRegistry
4936
*/
@@ -68,31 +55,14 @@ public function __construct(
6855
CustomerRepositoryInterface $customerRepository,
6956
ServiceOutputProcessor $serviceOutputProcessor,
7057
SerializerInterface $jsonSerializer,
71-
SubscriberFactory $subscriberFactory,
7258
CustomerRegistry $customerRegistry,
73-
Encryptor $encryptor,
74-
StoreResolverInterface $storeResolver
59+
Encryptor $encryptor
7560
) {
7661
$this->customerRepository = $customerRepository;
7762
$this->serviceOutputProcessor = $serviceOutputProcessor;
7863
$this->jsonSerializer = $jsonSerializer;
79-
$this->subscriberFactory = $subscriberFactory;
8064
$this->customerRegistry = $customerRegistry;
8165
$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);
9666
}
9767

9868
/**
@@ -158,82 +128,14 @@ private function processCustomer(CustomerInterface $customerObject): array
158128
}
159129

160130
/**
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-
*
131+
* @param string $password
175132
* @param int $customerId
176-
* @param $newSubscriptionStatus
177133
* @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
197134
* @throws NoSuchEntityException
198-
* @throws \Magento\Framework\Exception\InputException
199-
* @throws \Magento\Framework\Exception\State\InputMismatchException
200135
*/
201-
public function updateAccountInformation(int $customerId, array $customerData): CustomerInterface
136+
public function isPasswordCorrect(string $password, int $customerId)
202137
{
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;
138+
$hash = $this->customerRegistry->retrieveSecureData($customerId)->getPasswordHash();
139+
return $this->encryptor->validateHash($password, $hash);
238140
}
239141
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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\Customer;
9+
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Customer\Api\Data\CustomerInterface;
14+
use Magento\Store\Api\StoreResolverInterface;
15+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
16+
use Magento\Newsletter\Model\SubscriberFactory;
17+
18+
/**
19+
* Customer field data provider, used for GraphQL request processing.
20+
*/
21+
class CustomerDataUpdater
22+
{
23+
/**
24+
* @var CustomerRepositoryInterface
25+
*/
26+
private $customerRepository;
27+
28+
/**
29+
* @var \Magento\Newsletter\Model\SubscriberFactory
30+
*/
31+
private $subscriberFactory;
32+
33+
/**
34+
* @var StoreResolverInterface
35+
*/
36+
private $storeResolver;
37+
38+
/**
39+
* @var CustomerDataProvider
40+
*/
41+
private $customerDataProvider;
42+
43+
/**
44+
* CustomerDataUpdater constructor.
45+
* @param CustomerRepositoryInterface $customerRepository
46+
* @param StoreResolverInterface $storeResolver
47+
*/
48+
public function __construct(
49+
CustomerRepositoryInterface $customerRepository,
50+
StoreResolverInterface $storeResolver,
51+
CustomerDataProvider $customerDataProvider,
52+
SubscriberFactory $subscriberFactory
53+
) {
54+
$this->customerRepository = $customerRepository;
55+
$this->storeResolver = $storeResolver;
56+
$this->customerDataProvider = $customerDataProvider;
57+
$this->subscriberFactory = $subscriberFactory;
58+
}
59+
60+
/**
61+
* Manage customer subscription. Subscribe OR unsubscribe if required. Return new subscription status
62+
*
63+
* @param int $customerId
64+
* @param $newSubscriptionStatus
65+
* @return bool
66+
*/
67+
public function manageSubscription(int $customerId, bool $newSubscriptionStatus): bool
68+
{
69+
$subscriber = $this->subscriberFactory->create()->loadByCustomerId($customerId);
70+
if ($newSubscriptionStatus === true && !$subscriber->isSubscribed()) {
71+
$this->subscriberFactory->create()->subscribeCustomerById($customerId);
72+
} elseif ($newSubscriptionStatus === false && $subscriber->isSubscribed()) {
73+
$this->subscriberFactory->create()->unsubscribeCustomerById($customerId);
74+
}
75+
/** Load subscribed again to get his new status after update subscription */
76+
$subscriber = $this->subscriberFactory->create()->loadByCustomerId($customerId);
77+
return $subscriber->isSubscribed();
78+
}
79+
80+
/**
81+
* @param int $customerId
82+
* @param array $customerData
83+
* @return CustomerInterface
84+
* @throws GraphQlAuthorizationException
85+
* @throws LocalizedException
86+
* @throws NoSuchEntityException
87+
* @throws \Magento\Framework\Exception\InputException
88+
* @throws \Magento\Framework\Exception\State\InputMismatchException
89+
*/
90+
public function updateAccountInformation(int $customerId, array $customerData): CustomerInterface
91+
{
92+
$customer = $this->customerRepository->getById($customerId);
93+
94+
if (isset($customerData['email'])
95+
&& $customer->getEmail() !== $customerData['email']
96+
&& isset($customerData['password'])) {
97+
if ($this->customerDataProvider->isPasswordCorrect($customerData['password'], $customerId)) {
98+
$customer->setEmail($customerData['email']);
99+
} else {
100+
throw new GraphQlAuthorizationException(__('Invalid current user password.'));
101+
}
102+
}
103+
104+
if (isset($customerData['firstname'])) {
105+
$customer->setFirstname($customerData['firstname']);
106+
}
107+
if (isset($customerData['lastname'])) {
108+
$customer->setLastname($customerData['lastname']);
109+
}
110+
111+
$customer->setStoreId($this->storeResolver->getCurrentStoreId());
112+
$this->customerRepository->save($customer);
113+
114+
return $customer;
115+
}
116+
}

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Authorization\Model\UserContextInterface;
1111
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1212
use Magento\CustomerGraphQl\Model\Resolver\Customer\CustomerDataProvider;
13+
use Magento\CustomerGraphQl\Model\Resolver\Customer\CustomerDataUpdater;
1314
use Magento\Framework\GraphQl\Config\Element\Field;
1415
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1516
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
@@ -27,6 +28,11 @@ class CustomerUpdate implements ResolverInterface
2728
*/
2829
private $customerResolver;
2930

31+
/**
32+
* @var CustomerDataUpdater
33+
*/
34+
private $customerUpdater;
35+
3036
/**
3137
* @var ValueFactory
3238
*/
@@ -48,12 +54,14 @@ class CustomerUpdate implements ResolverInterface
4854
*/
4955
public function __construct(
5056
CustomerDataProvider $customerResolver,
57+
CustomerDataUpdater $customerUpdater,
5158
ValueFactory $valueFactory,
5259
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
5360
) {
5461
$this->customerResolver = $customerResolver;
5562
$this->valueFactory = $valueFactory;
5663
$this->subscriberFactory = $subscriberFactory;
64+
$this->customerUpdater = $customerUpdater;
5765
}
5866

5967
/**
@@ -77,13 +85,14 @@ public function resolve(
7785
);
7886
}
7987

80-
$this->customerResolver->updateAccountInformation($context->getUserId(), $args);
88+
$customerId = $context->getUserId();
89+
$this->customerUpdater->updateAccountInformation($customerId, $args);
90+
$data = $this->customerResolver->getCustomerById($customerId);
8191

8292
if (isset($args['is_subscribed'])) {
83-
$this->customerResolver->manageSubscription($context->getUserId(), $args['is_subscribed']);
93+
$data['is_subscribed'] = $this->customerUpdater->manageSubscription($customerId, $args['is_subscribed']);
8494
}
8595

86-
$data = $args;
8796
$result = function () use ($data) {
8897
return !empty($data) ? $data : [];
8998
};

app/code/Magento/CustomerGraphQl/composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"php": "~7.1.3||~7.2.0",
77
"magento/module-customer": "*",
88
"magento/module-authorization": "*",
9+
"magento/newsletter": "*",
910
"magento/framework": "*"
1011
},
1112
"suggest": {

0 commit comments

Comments
 (0)