Skip to content

Commit 8963811

Browse files
committed
Code refactoring
1 parent ef7f579 commit 8963811

File tree

2 files changed

+132
-51
lines changed

2 files changed

+132
-51
lines changed

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

+129-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
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;
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;
1621

1722
/**
1823
* Customer field data provider, used for GraphQL request processing.
@@ -29,11 +34,31 @@ class CustomerDataProvider
2934
*/
3035
private $serviceOutputProcessor;
3136

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+
3252
/**
3353
* @var SerializerInterface
3454
*/
3555
private $jsonSerializer;
3656

57+
/**
58+
* @var Encryptor
59+
*/
60+
protected $encryptor;
61+
3762
/**
3863
* @param CustomerRepositoryInterface $customerRepository
3964
* @param ServiceOutputProcessor $serviceOutputProcessor
@@ -42,11 +67,32 @@ class CustomerDataProvider
4267
public function __construct(
4368
CustomerRepositoryInterface $customerRepository,
4469
ServiceOutputProcessor $serviceOutputProcessor,
45-
SerializerInterface $jsonSerializer
70+
SerializerInterface $jsonSerializer,
71+
SubscriberFactory $subscriberFactory,
72+
CustomerRegistry $customerRegistry,
73+
Encryptor $encryptor,
74+
StoreResolverInterface $storeResolver
4675
) {
4776
$this->customerRepository = $customerRepository;
4877
$this->serviceOutputProcessor = $serviceOutputProcessor;
4978
$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);
5096
}
5197

5298
/**
@@ -56,7 +102,7 @@ public function __construct(
56102
* @return array
57103
* @throws NoSuchEntityException|LocalizedException
58104
*/
59-
public function getCustomerById(int $customerId) : array
105+
public function getCustomerById(int $customerId): array
60106
{
61107
try {
62108
$customerObject = $this->customerRepository->getById($customerId);
@@ -73,7 +119,7 @@ public function getCustomerById(int $customerId) : array
73119
* @param CustomerInterface $customerObject
74120
* @return array
75121
*/
76-
private function processCustomer(CustomerInterface $customerObject) : array
122+
private function processCustomer(CustomerInterface $customerObject): array
77123
{
78124
$customer = $this->serviceOutputProcessor->process(
79125
$customerObject,
@@ -110,4 +156,84 @@ private function processCustomer(CustomerInterface $customerObject) : array
110156

111157
return $customer;
112158
}
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+
}
113239
}

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

+3-48
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
use Magento\Framework\GraphQl\Query\Resolver\Value;
1717
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
1818
use Magento\Framework\GraphQl\Query\ResolverInterface;
19-
use Magento\Store\Api\StoreResolverInterface;
20-
use Magento\Framework\Encryption\EncryptorInterface as Encryptor;
21-
use Magento\Customer\Model\CustomerRegistry;
2219

2320
/**
2421
* Customers field resolver, used for GraphQL request processing.
@@ -35,11 +32,6 @@ class CustomerUpdate implements ResolverInterface
3532
*/
3633
private $valueFactory;
3734

38-
/**
39-
* @var StoreResolverInterface
40-
*/
41-
private $storeResolver;
42-
4335
/**
4436
* @var \Magento\Newsletter\Model\SubscriberFactory
4537
*/
@@ -50,31 +42,18 @@ class CustomerUpdate implements ResolverInterface
5042
*/
5143
protected $customerRegistry;
5244

53-
/**
54-
* @var Encryptor
55-
*/
56-
protected $encryptor;
57-
5845
/**
5946
* @param CustomerDataProvider $customerResolver
6047
* @param ValueFactory $valueFactory
6148
*/
6249
public function __construct(
6350
CustomerDataProvider $customerResolver,
6451
ValueFactory $valueFactory,
65-
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
66-
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
67-
StoreResolverInterface $storeResolver,
68-
Encryptor $encryptor,
69-
CustomerRegistry $customerRegistry
52+
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
7053
) {
7154
$this->customerResolver = $customerResolver;
7255
$this->valueFactory = $valueFactory;
73-
$this->customerRepository = $customerRepository;
7456
$this->subscriberFactory = $subscriberFactory;
75-
$this->storeResolver = $storeResolver;
76-
$this->encryptor = $encryptor;
77-
$this->customerRegistry = $customerRegistry;
7857
}
7958

8059
/**
@@ -98,34 +77,10 @@ public function resolve(
9877
);
9978
}
10079

101-
$customer = $this->customerRepository->getById($context->getUserId());
102-
103-
if (isset($args['email']) && $customer->getEmail() !== $args['email']) {
104-
$customerSecure = $this->customerRegistry->retrieveSecureData($context->getUserId());
105-
$hash = $customerSecure->getPasswordHash();
106-
if (!$this->encryptor->validateHash($args['password'], $hash)) {
107-
throw new GraphQlAuthorizationException(__('Invalid login or password.'));
108-
}
109-
$customer->setEmail($args['email']);
110-
}
111-
112-
if (isset($args['firstname'])) {
113-
$customer->setFirstname($args['firstname']);
114-
}
115-
if (isset($args['lastname'])) {
116-
$customer->setLastname($args['lastname']);
117-
}
118-
119-
$customer->setStoreId($this->storeResolver->getCurrentStoreId());
120-
$this->customerRepository->save($customer);
80+
$this->customerResolver->updateAccountInformation($context->getUserId(), $args);
12181

12282
if (isset($args['is_subscribed'])) {
123-
$checkSubscriber = $this->subscriberFactory->create()->loadByCustomerId($context->getUserId());
124-
if ($args['is_subscribed'] === true && !$checkSubscriber->isSubscribed()) {
125-
$this->subscriberFactory->create()->subscribeCustomerById($context->getUserId());
126-
} elseif ($args['is_subscribed'] === false && $checkSubscriber->isSubscribed()) {
127-
$this->subscriberFactory->create()->unsubscribeCustomerById($context->getUserId());
128-
}
83+
$this->customerResolver->manageSubscription($context->getUserId(), $args['is_subscribed']);
12984
}
13085

13186
$data = $args;

0 commit comments

Comments
 (0)