Skip to content

Commit 5b03488

Browse files
committed
GraphQL-468: Resolve coupling between objects in \Magento\QuoteGraphQl\Model\Cart\SetBillingAddressOnCart
1 parent ace616f commit 5b03488

File tree

4 files changed

+52
-102
lines changed

4 files changed

+52
-102
lines changed

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

Lines changed: 0 additions & 71 deletions
This file was deleted.

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10-
use Magento\Customer\Api\Data\AddressInterface as CustomerAddress;
10+
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
1111
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1213
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1315
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1416
use Magento\Quote\Model\Quote\AddressFactory as BaseQuoteAddressFactory;
1517

@@ -23,13 +25,21 @@ class QuoteAddressFactory
2325
*/
2426
private $quoteAddressFactory;
2527

28+
/**
29+
* @var GetCustomerAddress
30+
*/
31+
private $getCustomerAddress;
32+
2633
/**
2734
* @param BaseQuoteAddressFactory $quoteAddressFactory
35+
* @param GetCustomerAddress $getCustomerAddress
2836
*/
2937
public function __construct(
30-
BaseQuoteAddressFactory $quoteAddressFactory
38+
BaseQuoteAddressFactory $quoteAddressFactory,
39+
GetCustomerAddress $getCustomerAddress
3140
) {
3241
$this->quoteAddressFactory = $quoteAddressFactory;
42+
$this->getCustomerAddress = $getCustomerAddress;
3343
}
3444

3545
/**
@@ -48,14 +58,19 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress
4858
}
4959

5060
/**
51-
* Create QuoteAddress based on CustomerAddress
61+
* Create Quote Address based on Customer Address
5262
*
53-
* @param CustomerAddress $customerAddress
63+
* @param int $customerAddressId
64+
* @param int $customerId
5465
* @return QuoteAddress
5566
* @throws GraphQlInputException
67+
* @throws GraphQlAuthorizationException
68+
* @throws GraphQlNoSuchEntityException
5669
*/
57-
public function createBasedOnCustomerAddress(CustomerAddress $customerAddress): QuoteAddress
70+
public function createBasedOnCustomerAddress(int $customerAddressId, int $customerId): QuoteAddress
5871
{
72+
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, $customerId);
73+
5974
$quoteAddress = $this->quoteAddressFactory->create();
6075
try {
6176
$quoteAddress->importCustomerAddressData($customerAddress);

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

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

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10+
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
1011
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
1112
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1213
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -20,25 +21,33 @@
2021
class SetBillingAddressOnCart
2122
{
2223
/**
23-
* @var AssignBillingAddressToCart
24+
* @var QuoteAddressFactory
2425
*/
25-
private $assignBillingAddressToCart;
26+
private $quoteAddressFactory;
2627

2728
/**
28-
* @var CreateQuoteAddressByCustomerAddress
29+
* @var GetCustomer
2930
*/
30-
private $createQuoteAddressByCustomerAddress;
31+
private $getCustomer;
32+
33+
/**
34+
* @var AssignBillingAddressToCart
35+
*/
36+
private $assignBillingAddressToCart;
3137

3238
/**
39+
* @param QuoteAddressFactory $quoteAddressFactory
40+
* @param GetCustomer $getCustomer
3341
* @param AssignBillingAddressToCart $assignBillingAddressToCart
34-
* @param CreateQuoteAddressByCustomerAddress $createQuoteAddressByCustomerAddress
3542
*/
3643
public function __construct(
37-
AssignBillingAddressToCart $assignBillingAddressToCart,
38-
CreateQuoteAddressByCustomerAddress $createQuoteAddressByCustomerAddress
44+
QuoteAddressFactory $quoteAddressFactory,
45+
GetCustomer $getCustomer,
46+
AssignBillingAddressToCart $assignBillingAddressToCart
3947
) {
48+
$this->quoteAddressFactory = $quoteAddressFactory;
49+
$this->getCustomer = $getCustomer;
4050
$this->assignBillingAddressToCart = $assignBillingAddressToCart;
41-
$this->createQuoteAddressByCustomerAddress = $createQuoteAddressByCustomerAddress;
4251
}
4352

4453
/**
@@ -49,9 +58,9 @@ public function __construct(
4958
* @param array $billingAddressInput
5059
* @return void
5160
* @throws GraphQlInputException
52-
* @throws GraphQlNoSuchEntityException
53-
* @throws GraphQlAuthorizationException
5461
* @throws GraphQlAuthenticationException
62+
* @throws GraphQlAuthorizationException
63+
* @throws GraphQlNoSuchEntityException
5564
*/
5665
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddressInput): void
5766
{
@@ -79,11 +88,15 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
7988
);
8089
}
8190

82-
$billingAddress = $this->createQuoteAddressByCustomerAddress->execute(
83-
$context,
84-
$customerAddressId,
85-
$addressInput
86-
);
91+
if (null === $customerAddressId) {
92+
$billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
93+
} else {
94+
$customer = $this->getCustomer->execute($context);
95+
$billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
96+
(int)$customerAddressId,
97+
(int)$customer->getId()
98+
);
99+
}
87100

88101
$this->assignBillingAddressToCart->execute($cart, $billingAddress, $useForShipping);
89102
}

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

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

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10-
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
1110
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
1211
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1312
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
@@ -28,11 +27,6 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
2827
*/
2928
private $getCustomer;
3029

31-
/**
32-
* @var GetCustomerAddress
33-
*/
34-
private $getCustomerAddress;
35-
3630
/**
3731
* @var AssignShippingAddressToCart
3832
*/
@@ -41,18 +35,15 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
4135
/**
4236
* @param QuoteAddressFactory $quoteAddressFactory
4337
* @param GetCustomer $getCustomer
44-
* @param GetCustomerAddress $getCustomerAddress
4538
* @param AssignShippingAddressToCart $assignShippingAddressToCart
4639
*/
4740
public function __construct(
4841
QuoteAddressFactory $quoteAddressFactory,
4942
GetCustomer $getCustomer,
50-
GetCustomerAddress $getCustomerAddress,
5143
AssignShippingAddressToCart $assignShippingAddressToCart
5244
) {
5345
$this->quoteAddressFactory = $quoteAddressFactory;
5446
$this->getCustomer = $getCustomer;
55-
$this->getCustomerAddress = $getCustomerAddress;
5647
$this->assignShippingAddressToCart = $assignShippingAddressToCart;
5748
}
5849

@@ -86,8 +77,10 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
8677
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
8778
} else {
8879
$customer = $this->getCustomer->execute($context);
89-
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$customer->getId());
90-
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress);
80+
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
81+
(int)$customerAddressId,
82+
(int)$customer->getId()
83+
);
9184
}
9285

9386
$this->assignShippingAddressToCart->execute($cart, $shippingAddress);

0 commit comments

Comments
 (0)