This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
/
SetBillingAddressOnCart.php
147 lines (130 loc) · 5.3 KB
/
SetBillingAddressOnCart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\QuoteGraphQl\Model\Cart;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address;
use Magento\QuoteGraphQl\Model\Cart\Address\SaveQuoteAddressToCustomerAddressBook;
/**
* Set billing address for a specified shopping cart
*/
class SetBillingAddressOnCart
{
/**
* @var QuoteAddressFactory
*/
private $quoteAddressFactory;
/**
* @var AssignBillingAddressToCart
*/
private $assignBillingAddressToCart;
/**
* @var SaveQuoteAddressToCustomerAddressBook
*/
private $saveQuoteAddressToCustomerAddressBook;
/**
* @param QuoteAddressFactory $quoteAddressFactory
* @param AssignBillingAddressToCart $assignBillingAddressToCart
* @param SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
*/
public function __construct(
QuoteAddressFactory $quoteAddressFactory,
AssignBillingAddressToCart $assignBillingAddressToCart,
SaveQuoteAddressToCustomerAddressBook $saveQuoteAddressToCustomerAddressBook
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->assignBillingAddressToCart = $assignBillingAddressToCart;
$this->saveQuoteAddressToCustomerAddressBook = $saveQuoteAddressToCustomerAddressBook;
}
/**
* Set billing address for a specified shopping cart
*
* @param ContextInterface $context
* @param CartInterface $cart
* @param array $billingAddressInput
* @return void
* @throws GraphQlAuthorizationException
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddressInput): void
{
$customerAddressId = $billingAddressInput['customer_address_id'] ?? null;
$addressInput = $billingAddressInput['address'] ?? null;
// Need to keep this for BC of `use_for_shipping` field
$sameAsShipping = isset($billingAddressInput['use_for_shipping'])
? (bool)$billingAddressInput['use_for_shipping'] : false;
$sameAsShipping = isset($billingAddressInput['same_as_shipping'])
? (bool)$billingAddressInput['same_as_shipping'] : $sameAsShipping;
if (null === $customerAddressId && null === $addressInput) {
throw new GraphQlInputException(
__('The billing address must contain either "customer_address_id" or "address".')
);
}
if ($customerAddressId && $addressInput) {
throw new GraphQlInputException(
__('The billing address cannot contain "customer_address_id" and "address" at the same time.')
);
}
$addresses = $cart->getAllShippingAddresses();
if ($sameAsShipping && count($addresses) > 1) {
throw new GraphQlInputException(
__('Using the "same_as_shipping" option with multishipping is not possible.')
);
}
$billingAddress = $this->createBillingAddress($context, $customerAddressId, $addressInput);
$this->assignBillingAddressToCart->execute($cart, $billingAddress, $sameAsShipping);
}
/**
* Create billing address
*
* @param ContextInterface $context
* @param int|null $customerAddressId
* @param array $addressInput
* @return Address
* @throws GraphQlAuthorizationException
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
private function createBillingAddress(
ContextInterface $context,
?int $customerAddressId,
?array $addressInput
): Address {
if (null === $customerAddressId) {
$billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
$customerId = $context->getUserId();
// need to save address only for registered user and if save_in_address_book = true
if (0 !== $customerId
&& isset($addressInput['save_in_address_book'])
&& (bool)$addressInput['save_in_address_book'] === true
) {
$this->saveQuoteAddressToCustomerAddressBook->execute($billingAddress, $customerId);
}
} else {
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}
$billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
(int)$customerAddressId,
(int)$context->getUserId()
);
}
$errors = $billingAddress->validate();
if (true !== $errors) {
$e = new GraphQlInputException(__('Billing address errors'));
foreach ($errors as $error) {
$e->addError(new GraphQlInputException($error));
}
throw $e;
}
return $billingAddress;
}
}