Skip to content

Commit 0402368

Browse files
committed
Add extension point for Quote Address.
1 parent d42c828 commit 0402368

File tree

2 files changed

+75
-25
lines changed

2 files changed

+75
-25
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
11+
use Magento\GraphQl\Model\Query\ContextInterface;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14+
use Magento\Quote\Model\Quote\Address;
15+
16+
/**
17+
* Get shipping address
18+
*/
19+
class GetShippingAddress
20+
{
21+
/**
22+
* @var QuoteAddressFactory
23+
*/
24+
private $quoteAddressFactory;
25+
26+
/**
27+
* @param QuoteAddressFactory $quoteAddressFactory
28+
*/
29+
public function __construct(QuoteAddressFactory $quoteAddressFactory)
30+
{
31+
$this->quoteAddressFactory = $quoteAddressFactory;
32+
}
33+
34+
/**
35+
* @param ContextInterface $context
36+
* @param array $shippingAddressInput
37+
* @return Address
38+
* @throws GraphQlAuthorizationException
39+
* @throws GraphQlInputException
40+
* @throws GraphQlNoSuchEntityException
41+
*/
42+
public function execute(ContextInterface $context, array $shippingAddressInput)
43+
{
44+
$customerAddressId = $shippingAddressInput['customer_address_id'] ?? null;
45+
46+
$addressInput = $shippingAddressInput['address'];
47+
if ($addressInput) {
48+
$addressInput['customer_notes'] = $shippingAddressInput['customer_notes'] ?? '';
49+
}
50+
51+
if (null === $customerAddressId) {
52+
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
53+
} else {
54+
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
55+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
56+
}
57+
58+
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
59+
(int)$customerAddressId,
60+
$context->getUserId()
61+
);
62+
}
63+
64+
return $shippingAddress;
65+
}
66+
}

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

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,25 @@
1717
*/
1818
class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
1919
{
20-
/**
21-
* @var QuoteAddressFactory
22-
*/
23-
private $quoteAddressFactory;
24-
2520
/**
2621
* @var AssignShippingAddressToCart
2722
*/
2823
private $assignShippingAddressToCart;
24+
/**
25+
* @var GetShippingAddress
26+
*/
27+
private $getShippingAddress;
2928

3029
/**
31-
* @param QuoteAddressFactory $quoteAddressFactory
3230
* @param AssignShippingAddressToCart $assignShippingAddressToCart
31+
* @param GetShippingAddress $getShippingAddress
3332
*/
3433
public function __construct(
35-
QuoteAddressFactory $quoteAddressFactory,
36-
AssignShippingAddressToCart $assignShippingAddressToCart
34+
AssignShippingAddressToCart $assignShippingAddressToCart,
35+
GetShippingAddress $getShippingAddress
3736
) {
38-
$this->quoteAddressFactory = $quoteAddressFactory;
3937
$this->assignShippingAddressToCart = $assignShippingAddressToCart;
38+
$this->getShippingAddress = $getShippingAddress;
4039
}
4140

4241
/**
@@ -53,10 +52,6 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
5352
$customerAddressId = $shippingAddressInput['customer_address_id'] ?? null;
5453
$addressInput = $shippingAddressInput['address'] ?? null;
5554

56-
if ($addressInput) {
57-
$addressInput['customer_notes'] = $shippingAddressInput['customer_notes'] ?? '';
58-
}
59-
6055
if (null === $customerAddressId && null === $addressInput) {
6156
throw new GraphQlInputException(
6257
__('The shipping address must contain either "customer_address_id" or "address".')
@@ -69,18 +64,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
6964
);
7065
}
7166

72-
if (null === $customerAddressId) {
73-
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
74-
} else {
75-
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
76-
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
77-
}
78-
79-
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
80-
(int)$customerAddressId,
81-
$context->getUserId()
82-
);
83-
}
67+
$shippingAddress = $this->getShippingAddress->execute($context, $shippingAddressInput);
8468

8569
$this->assignShippingAddressToCart->execute($cart, $shippingAddress);
8670
}

0 commit comments

Comments
 (0)