Skip to content

Commit f40faed

Browse files
committed
GraphQL-458: CustomerGraphQl module refactoring
1 parent c201238 commit f40faed

File tree

4 files changed

+70
-41
lines changed

4 files changed

+70
-41
lines changed

app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public function execute(array $data): CustomerInterface
8888
}
8989

9090
/**
91+
* Create account
92+
*
9193
* @param array $data
9294
* @return CustomerInterface
9395
* @throws LocalizedException
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\Api\CustomerRepositoryInterface;
11+
use Magento\Framework\Exception\AlreadyExistsException;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
15+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
16+
use Magento\Customer\Api\Data\CustomerInterface;
17+
18+
/**
19+
* Save customer
20+
*/
21+
class SaveCustomer
22+
{
23+
/**
24+
* @var CustomerRepositoryInterface
25+
*/
26+
private $customerRepository;
27+
28+
/**
29+
* @param CustomerRepositoryInterface $customerRepository
30+
*/
31+
public function __construct(
32+
CustomerRepositoryInterface $customerRepository
33+
) {
34+
$this->customerRepository = $customerRepository;
35+
}
36+
37+
/**
38+
* Save customer
39+
*
40+
* @param CustomerInterface $customer
41+
* @param array $data
42+
* @throws GraphQlAlreadyExistsException
43+
* @throws GraphQlAuthenticationException
44+
* @throws GraphQlInputException
45+
*/
46+
public function execute(CustomerInterface $customer): void
47+
{
48+
try {
49+
$this->customerRepository->save($customer);
50+
} catch (AlreadyExistsException $e) {
51+
throw new GraphQlAlreadyExistsException(
52+
__('A customer with the same email address already exists in an associated website.'),
53+
$e
54+
);
55+
} catch (LocalizedException $e) {
56+
throw new GraphQlInputException(__($e->getMessage()), $e);
57+
}
58+
}
59+
}

app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
namespace Magento\CustomerGraphQl\Model\Customer;
99

10-
use Magento\Customer\Api\CustomerRepositoryInterface;
11-
use Magento\Framework\Exception\AlreadyExistsException;
12-
use Magento\Framework\Exception\LocalizedException;
1310
use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException;
1411
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
1512
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -23,9 +20,9 @@
2320
class UpdateCustomerAccount
2421
{
2522
/**
26-
* @var CustomerRepositoryInterface
23+
* @var SaveCustomer
2724
*/
28-
private $customerRepository;
25+
private $saveCustomer;
2926

3027
/**
3128
* @var StoreManagerInterface
@@ -53,22 +50,22 @@ class UpdateCustomerAccount
5350
private $restrictedKeys;
5451

5552
/**
56-
* @param CustomerRepositoryInterface $customerRepository
53+
* @param SaveCustomer $saveCustomer
5754
* @param StoreManagerInterface $storeManager
5855
* @param CheckCustomerPassword $checkCustomerPassword
5956
* @param DataObjectHelper $dataObjectHelper
6057
* @param ChangeSubscriptionStatus $changeSubscriptionStatus
6158
* @param array $restrictedKeys
6259
*/
6360
public function __construct(
64-
CustomerRepositoryInterface $customerRepository,
61+
SaveCustomer $saveCustomer,
6562
StoreManagerInterface $storeManager,
6663
CheckCustomerPassword $checkCustomerPassword,
6764
DataObjectHelper $dataObjectHelper,
6865
ChangeSubscriptionStatus $changeSubscriptionStatus,
6966
array $restrictedKeys = []
7067
) {
71-
$this->customerRepository = $customerRepository;
68+
$this->saveCustomer = $saveCustomer;
7269
$this->storeManager = $storeManager;
7370
$this->checkCustomerPassword = $checkCustomerPassword;
7471
$this->dataObjectHelper = $dataObjectHelper;
@@ -87,26 +84,6 @@ public function __construct(
8784
* @throws GraphQlInputException
8885
*/
8986
public function execute(CustomerInterface $customer, array $data): void
90-
{
91-
try {
92-
$this->updateCustomer($customer, $data);
93-
} catch (LocalizedException $e) {
94-
throw new GraphQlInputException(__($e->getMessage()));
95-
}
96-
97-
if (isset($data['is_subscribed'])) {
98-
$this->changeSubscriptionStatus->execute((int)$customer->getId(), (bool)$data['is_subscribed']);
99-
}
100-
}
101-
102-
/**
103-
* @param CustomerInterface $customer
104-
* @param array $data
105-
* @throws GraphQlAlreadyExistsException
106-
* @throws GraphQlAuthenticationException
107-
* @throws GraphQlInputException
108-
*/
109-
private function updateCustomer(CustomerInterface $customer, array $data): void
11087
{
11188
if (isset($data['email']) && $customer->getEmail() !== $data['email']) {
11289
if (!isset($data['password']) || empty($data['password'])) {
@@ -122,15 +99,10 @@ private function updateCustomer(CustomerInterface $customer, array $data): void
12299

123100
$customer->setStoreId($this->storeManager->getStore()->getId());
124101

125-
try {
126-
$this->customerRepository->save($customer);
127-
} catch (AlreadyExistsException $e) {
128-
throw new GraphQlAlreadyExistsException(
129-
__('A customer with the same email address already exists in an associated website.'),
130-
$e
131-
);
132-
} catch (LocalizedException $e) {
133-
throw new GraphQlInputException(__($e->getMessage()), $e);
102+
$this->saveCustomer->execute($customer);
103+
104+
if (isset($data['is_subscribed'])) {
105+
$this->changeSubscriptionStatus->execute((int)$customer->getId(), (bool)$data['is_subscribed']);
134106
}
135107
}
136108
}

app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
1111
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
12-
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
13-
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1412
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1513
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1614
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
@@ -67,8 +65,6 @@ public function __construct(
6765
* @param array $billingAddressInput
6866
* @return void
6967
* @throws GraphQlInputException
70-
* @throws GraphQlAuthenticationException
71-
* @throws GraphQlAuthorizationException
7268
* @throws GraphQlNoSuchEntityException
7369
*/
7470
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddressInput): void

0 commit comments

Comments
 (0)