Skip to content

Commit 2c0036b

Browse files
ENGCOM-5983: Add shipping address input extension #955
- Merge Pull Request magento/graphql-ce#955 from swnsma/graphql-ce:Add-Shipping-Address-Input-Extension - Merged commits: 1. d42c828 2. 0402368 3. be027d8
2 parents 6f1a6f7 + be027d8 commit 2c0036b

File tree

2 files changed

+89
-39
lines changed

2 files changed

+89
-39
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
* Get Shipping Address based on the input.
36+
*
37+
* @param ContextInterface $context
38+
* @param array $shippingAddressInput
39+
* @return Address
40+
* @throws GraphQlAuthorizationException
41+
* @throws GraphQlInputException
42+
* @throws GraphQlNoSuchEntityException
43+
*/
44+
public function execute(ContextInterface $context, array $shippingAddressInput): Address
45+
{
46+
$customerAddressId = $shippingAddressInput['customer_address_id'] ?? null;
47+
$addressInput = $shippingAddressInput['address'] ?? null;
48+
49+
if ($addressInput) {
50+
$addressInput['customer_notes'] = $shippingAddressInput['customer_notes'] ?? '';
51+
}
52+
53+
if (null === $customerAddressId && null === $addressInput) {
54+
throw new GraphQlInputException(
55+
__('The shipping address must contain either "customer_address_id" or "address".')
56+
);
57+
}
58+
59+
if ($customerAddressId && $addressInput) {
60+
throw new GraphQlInputException(
61+
__('The shipping address cannot contain "customer_address_id" and "address" at the same time.')
62+
);
63+
}
64+
65+
if (null === $customerAddressId) {
66+
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
67+
} else {
68+
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
69+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
70+
}
71+
72+
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
73+
(int)$customerAddressId,
74+
$context->getUserId()
75+
);
76+
}
77+
78+
return $shippingAddress;
79+
}
80+
}

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

Lines changed: 9 additions & 39 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
/**
@@ -50,37 +49,8 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
5049
);
5150
}
5251
$shippingAddressInput = current($shippingAddressesInput);
53-
$customerAddressId = $shippingAddressInput['customer_address_id'] ?? null;
54-
$addressInput = $shippingAddressInput['address'] ?? null;
55-
56-
if ($addressInput) {
57-
$addressInput['customer_notes'] = $shippingAddressInput['customer_notes'] ?? '';
58-
}
59-
60-
if (null === $customerAddressId && null === $addressInput) {
61-
throw new GraphQlInputException(
62-
__('The shipping address must contain either "customer_address_id" or "address".')
63-
);
64-
}
6552

66-
if ($customerAddressId && $addressInput) {
67-
throw new GraphQlInputException(
68-
__('The shipping address cannot contain "customer_address_id" and "address" at the same time.')
69-
);
70-
}
71-
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-
}
53+
$shippingAddress = $this->getShippingAddress->execute($context, $shippingAddressInput);
8454

8555
$this->assignShippingAddressToCart->execute($cart, $shippingAddress);
8656
}

0 commit comments

Comments
 (0)