Skip to content

Commit 0c73d54

Browse files
ENGCOM-3139: My Account > Change account information and Newsletter subscription #162
- Merge Pull Request magento/graphql-ce#162 from comwrap/graphql-ce:55-56-account-mutations - Merged commits: 1. 09dc09c 2. ef7f579 3. 8963811 4. d0dd90f 5. b691d84 6. d759867 7. a1dd683 8. cc8d328 9. 9200a16 10. 2057ddf 11. 20e3393 12. 661988b 13. 706af91 14. a1c4d33 15. 0cb378b 16. 6e6b5c3 17. 6f73325 18. 7ca6097
2 parents 80469a6 + 7ca6097 commit 0c73d54

31 files changed

+1396
-440
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\Customer;
9+
10+
use Magento\Newsletter\Model\SubscriberFactory;
11+
12+
/**
13+
* Change subscription status. Subscribe OR unsubscribe if required
14+
*/
15+
class ChangeSubscriptionStatus
16+
{
17+
/**
18+
* @var SubscriberFactory
19+
*/
20+
private $subscriberFactory;
21+
22+
/**
23+
* @param SubscriberFactory $subscriberFactory
24+
*/
25+
public function __construct(
26+
SubscriberFactory $subscriberFactory
27+
) {
28+
$this->subscriberFactory = $subscriberFactory;
29+
}
30+
31+
/**
32+
* Change subscription status. Subscribe OR unsubscribe if required
33+
*
34+
* @param int $customerId
35+
* @param bool $subscriptionStatus
36+
* @return void
37+
*/
38+
public function execute(int $customerId, bool $subscriptionStatus): void
39+
{
40+
$subscriber = $this->subscriberFactory->create()->loadByCustomerId($customerId);
41+
42+
if ($subscriptionStatus === true && !$subscriber->isSubscribed()) {
43+
$this->subscriberFactory->create()->subscribeCustomerById($customerId);
44+
} elseif ($subscriptionStatus === false && $subscriber->isSubscribed()) {
45+
$this->subscriberFactory->create()->unsubscribeCustomerById($customerId);
46+
}
47+
}
48+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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\Customer;
9+
10+
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Customer\Api\AccountManagementInterface;
12+
use Magento\Customer\Api\CustomerRepositoryInterface;
13+
use Magento\Customer\Model\AuthenticationInterface;
14+
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
16+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
17+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
18+
19+
/**
20+
* Check customer account
21+
*/
22+
class CheckCustomerAccount
23+
{
24+
/**
25+
* @var AuthenticationInterface
26+
*/
27+
private $authentication;
28+
29+
/**
30+
* @var CustomerRepositoryInterface
31+
*/
32+
private $customerRepository;
33+
34+
/**
35+
* @var AccountManagementInterface
36+
*/
37+
private $accountManagement;
38+
39+
/**
40+
* @param AuthenticationInterface $authentication
41+
* @param CustomerRepositoryInterface $customerRepository
42+
* @param AccountManagementInterface $accountManagement
43+
*/
44+
public function __construct(
45+
AuthenticationInterface $authentication,
46+
CustomerRepositoryInterface $customerRepository,
47+
AccountManagementInterface $accountManagement
48+
) {
49+
$this->authentication = $authentication;
50+
$this->customerRepository = $customerRepository;
51+
$this->accountManagement = $accountManagement;
52+
}
53+
54+
/**
55+
* Check customer account
56+
*
57+
* @param int|null $customerId
58+
* @param int|null $customerType
59+
* @return void
60+
* @throws GraphQlAuthorizationException
61+
* @throws GraphQlNoSuchEntityException
62+
* @throws GraphQlAuthenticationException
63+
*/
64+
public function execute(?int $customerId, ?int $customerType): void
65+
{
66+
if (true === $this->isCustomerGuest($customerId, $customerType)) {
67+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
68+
}
69+
70+
try {
71+
$this->customerRepository->getById($customerId);
72+
} catch (NoSuchEntityException $e) {
73+
throw new GraphQlNoSuchEntityException(
74+
__('Customer with id "%customer_id" does not exist.', ['customer_id' => $customerId]),
75+
$e
76+
);
77+
}
78+
79+
if (true === $this->authentication->isLocked($customerId)) {
80+
throw new GraphQlAuthenticationException(__('The account is locked.'));
81+
}
82+
83+
$confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId);
84+
if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) {
85+
throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again."));
86+
}
87+
}
88+
89+
/**
90+
* Checking if current customer is guest
91+
*
92+
* @param int|null $customerId
93+
* @param int|null $customerType
94+
* @return bool
95+
*/
96+
private function isCustomerGuest(?int $customerId, ?int $customerType): bool
97+
{
98+
if (null === $customerId || null === $customerType) {
99+
return true;
100+
}
101+
return 0 === (int)$customerId || (int)$customerType === UserContextInterface::USER_TYPE_GUEST;
102+
}
103+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Customer;
9+
10+
use Magento\Customer\Model\AuthenticationInterface;
11+
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
13+
14+
/**
15+
* Check customer password
16+
*/
17+
class CheckCustomerPassword
18+
{
19+
/**
20+
* @var AuthenticationInterface
21+
*/
22+
private $authentication;
23+
24+
/**
25+
* @param AuthenticationInterface $authentication
26+
*/
27+
public function __construct(
28+
AuthenticationInterface $authentication
29+
) {
30+
$this->authentication = $authentication;
31+
}
32+
33+
/**
34+
* Check customer password
35+
*
36+
* @param string $password
37+
* @param int $customerId
38+
* @throws GraphQlAuthenticationException
39+
*/
40+
public function execute(string $password, int $customerId)
41+
{
42+
try {
43+
$this->authentication->authenticate($customerId, $password);
44+
} catch (InvalidEmailOrPasswordException $e) {
45+
throw new GraphQlAuthenticationException(
46+
__('The password doesn\'t match this account. Verify the password and try again.')
47+
);
48+
}
49+
}
50+
}

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\CustomerGraphQl\Model\Resolver\Customer;
8+
namespace Magento\CustomerGraphQl\Model\Customer;
99

1010
use Magento\Customer\Api\CustomerRepositoryInterface;
1111
use Magento\Framework\Exception\LocalizedException;
1212
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1314
use Magento\Framework\Serialize\SerializerInterface;
1415
use Magento\Framework\Webapi\ServiceOutputProcessor;
1516
use Magento\Customer\Api\Data\CustomerInterface;
@@ -32,21 +33,21 @@ class CustomerDataProvider
3233
/**
3334
* @var SerializerInterface
3435
*/
35-
private $jsonSerializer;
36+
private $serializer;
3637

3738
/**
3839
* @param CustomerRepositoryInterface $customerRepository
3940
* @param ServiceOutputProcessor $serviceOutputProcessor
40-
* @param SerializerInterface $jsonSerializer
41+
* @param SerializerInterface $serializer
4142
*/
4243
public function __construct(
4344
CustomerRepositoryInterface $customerRepository,
4445
ServiceOutputProcessor $serviceOutputProcessor,
45-
SerializerInterface $jsonSerializer
46+
SerializerInterface $serializer
4647
) {
4748
$this->customerRepository = $customerRepository;
4849
$this->serviceOutputProcessor = $serviceOutputProcessor;
49-
$this->jsonSerializer = $jsonSerializer;
50+
$this->serializer = $serializer;
5051
}
5152

5253
/**
@@ -56,42 +57,44 @@ public function __construct(
5657
* @return array
5758
* @throws NoSuchEntityException|LocalizedException
5859
*/
59-
public function getCustomerById(int $customerId) : array
60+
public function getCustomerById(int $customerId): array
6061
{
6162
try {
62-
$customerObject = $this->customerRepository->getById($customerId);
63+
$customer = $this->customerRepository->getById($customerId);
6364
} catch (NoSuchEntityException $e) {
64-
// No error should be thrown, null result should be returned
65-
return [];
65+
throw new GraphQlNoSuchEntityException(
66+
__('Customer id "%customer_id" does not exist.', ['customer_id' => $customerId]),
67+
$e
68+
);
6669
}
67-
return $this->processCustomer($customerObject);
70+
return $this->processCustomer($customer);
6871
}
6972

7073
/**
7174
* Transform single customer data from object to in array format
7275
*
73-
* @param CustomerInterface $customerObject
76+
* @param CustomerInterface $customer
7477
* @return array
7578
*/
76-
private function processCustomer(CustomerInterface $customerObject) : array
79+
private function processCustomer(CustomerInterface $customer): array
7780
{
78-
$customer = $this->serviceOutputProcessor->process(
79-
$customerObject,
81+
$customerData = $this->serviceOutputProcessor->process(
82+
$customer,
8083
CustomerRepositoryInterface::class,
8184
'get'
8285
);
83-
if (isset($customer['extension_attributes'])) {
84-
$customer = array_merge($customer, $customer['extension_attributes']);
86+
if (isset($customerData['extension_attributes'])) {
87+
$customerData = array_merge($customerData, $customerData['extension_attributes']);
8588
}
8689
$customAttributes = [];
87-
if (isset($customer['custom_attributes'])) {
88-
foreach ($customer['custom_attributes'] as $attribute) {
90+
if (isset($customerData['custom_attributes'])) {
91+
foreach ($customerData['custom_attributes'] as $attribute) {
8992
$isArray = false;
9093
if (is_array($attribute['value'])) {
9194
$isArray = true;
9295
foreach ($attribute['value'] as $attributeValue) {
9396
if (is_array($attributeValue)) {
94-
$customAttributes[$attribute['attribute_code']] = $this->jsonSerializer->serialize(
97+
$customAttributes[$attribute['attribute_code']] = $this->serializer->serialize(
9598
$attribute['value']
9699
);
97100
continue;
@@ -106,8 +109,8 @@ private function processCustomer(CustomerInterface $customerObject) : array
106109
$customAttributes[$attribute['attribute_code']] = $attribute['value'];
107110
}
108111
}
109-
$customer = array_merge($customer, $customAttributes);
112+
$customerData = array_merge($customerData, $customAttributes);
110113

111-
return $customer;
114+
return $customerData;
112115
}
113116
}

0 commit comments

Comments
 (0)