6
6
7
7
namespace Magento \Checkout \Model ;
8
8
9
+ use Magento \Checkout \Api \Data \PaymentDetailsInterface ;
10
+ use Magento \Checkout \Api \Data \ShippingInformationInterface ;
11
+ use Magento \Checkout \Api \ShippingInformationManagementInterface ;
12
+ use Magento \Customer \Api \AddressRepositoryInterface ;
13
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
14
+ use Magento \Framework \App \ObjectManager ;
9
15
use Magento \Framework \Exception \InputException ;
10
- use Magento \Framework \Exception \StateException ;
16
+ use Magento \Framework \Exception \LocalizedException ;
11
17
use Magento \Framework \Exception \NoSuchEntityException ;
18
+ use Magento \Framework \Exception \StateException ;
19
+ use Magento \Quote \Api \CartRepositoryInterface ;
20
+ use Magento \Quote \Api \CartTotalRepositoryInterface ;
12
21
use Magento \Quote \Api \Data \AddressInterface ;
22
+ use Magento \Quote \Api \Data \CartExtensionFactory ;
13
23
use Magento \Quote \Api \Data \CartInterface ;
14
- use Psr \Log \LoggerInterface as Logger ;
24
+ use Magento \Quote \Api \PaymentMethodManagementInterface ;
25
+ use Magento \Quote \Model \Quote ;
26
+ use Magento \Quote \Model \Quote \TotalsCollector ;
15
27
use Magento \Quote \Model \QuoteAddressValidator ;
16
- use Magento \Quote \Api \Data \CartExtensionFactory ;
17
28
use Magento \Quote \Model \ShippingAssignmentFactory ;
18
29
use Magento \Quote \Model \ShippingFactory ;
19
- use Magento \ Framework \ App \ ObjectManager ;
30
+ use Psr \ Log \ LoggerInterface as Logger ;
20
31
21
32
/**
22
- * Class ShippingInformationManagement
33
+ * Class checkout shipping information management
23
34
*
24
- * @package Magento\Checkout\Model
25
35
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
26
36
*/
27
- class ShippingInformationManagement implements \ Magento \ Checkout \ Api \ ShippingInformationManagementInterface
37
+ class ShippingInformationManagement implements ShippingInformationManagementInterface
28
38
{
29
39
/**
30
- * @var \Magento\Quote\Api\ PaymentMethodManagementInterface
40
+ * @var PaymentMethodManagementInterface
31
41
*/
32
42
protected $ paymentMethodManagement ;
33
43
@@ -37,12 +47,12 @@ class ShippingInformationManagement implements \Magento\Checkout\Api\ShippingInf
37
47
protected $ paymentDetailsFactory ;
38
48
39
49
/**
40
- * @var \Magento\Quote\Api\ CartTotalRepositoryInterface
50
+ * @var CartTotalRepositoryInterface
41
51
*/
42
52
protected $ cartTotalsRepository ;
43
53
44
54
/**
45
- * @var \Magento\Quote\Api\ CartRepositoryInterface
55
+ * @var CartRepositoryInterface
46
56
*/
47
57
protected $ quoteRepository ;
48
58
@@ -57,65 +67,63 @@ class ShippingInformationManagement implements \Magento\Checkout\Api\ShippingInf
57
67
protected $ addressValidator ;
58
68
59
69
/**
60
- * @var \Magento\Customer\Api\ AddressRepositoryInterface
70
+ * @var AddressRepositoryInterface
61
71
* @deprecated 100.2.0
62
72
*/
63
73
protected $ addressRepository ;
64
74
65
75
/**
66
- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
76
+ * @var ScopeConfigInterface
67
77
* @deprecated 100.2.0
68
78
*/
69
79
protected $ scopeConfig ;
70
80
71
81
/**
72
- * @var \Magento\Quote\Model\Quote\ TotalsCollector
82
+ * @var TotalsCollector
73
83
* @deprecated 100.2.0
74
84
*/
75
85
protected $ totalsCollector ;
76
86
77
87
/**
78
- * @var \Magento\Quote\Api\Data\ CartExtensionFactory
88
+ * @var CartExtensionFactory
79
89
*/
80
90
private $ cartExtensionFactory ;
81
91
82
92
/**
83
- * @var \Magento\Quote\Model\ ShippingAssignmentFactory
93
+ * @var ShippingAssignmentFactory
84
94
*/
85
95
protected $ shippingAssignmentFactory ;
86
96
87
97
/**
88
- * @var \Magento\Quote\Model\ ShippingFactory
98
+ * @var ShippingFactory
89
99
*/
90
100
private $ shippingFactory ;
91
101
92
102
/**
93
- * Constructor
94
- *
95
- * @param \Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement
96
- * @param \Magento\Checkout\Model\PaymentDetailsFactory $paymentDetailsFactory
97
- * @param \Magento\Quote\Api\CartTotalRepositoryInterface $cartTotalsRepository
98
- * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
99
- * @param \Magento\Quote\Model\QuoteAddressValidator $addressValidator
103
+ * @param PaymentMethodManagementInterface $paymentMethodManagement
104
+ * @param PaymentDetailsFactory $paymentDetailsFactory
105
+ * @param CartTotalRepositoryInterface $cartTotalsRepository
106
+ * @param CartRepositoryInterface $quoteRepository
107
+ * @param QuoteAddressValidator $addressValidator
100
108
* @param Logger $logger
101
- * @param \Magento\Customer\Api\ AddressRepositoryInterface $addressRepository
102
- * @param \Magento\Framework\App\Config\ ScopeConfigInterface $scopeConfig
103
- * @param \Magento\Quote\Model\Quote\ TotalsCollector $totalsCollector
109
+ * @param AddressRepositoryInterface $addressRepository
110
+ * @param ScopeConfigInterface $scopeConfig
111
+ * @param TotalsCollector $totalsCollector
104
112
* @param CartExtensionFactory|null $cartExtensionFactory
105
113
* @param ShippingAssignmentFactory|null $shippingAssignmentFactory
106
114
* @param ShippingFactory|null $shippingFactory
107
115
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
108
116
*/
109
117
public function __construct (
110
- \ Magento \ Quote \ Api \ PaymentMethodManagementInterface $ paymentMethodManagement ,
111
- \ Magento \ Checkout \ Model \ PaymentDetailsFactory $ paymentDetailsFactory ,
112
- \ Magento \ Quote \ Api \ CartTotalRepositoryInterface $ cartTotalsRepository ,
113
- \ Magento \ Quote \ Api \ CartRepositoryInterface $ quoteRepository ,
118
+ PaymentMethodManagementInterface $ paymentMethodManagement ,
119
+ PaymentDetailsFactory $ paymentDetailsFactory ,
120
+ CartTotalRepositoryInterface $ cartTotalsRepository ,
121
+ CartRepositoryInterface $ quoteRepository ,
114
122
QuoteAddressValidator $ addressValidator ,
115
123
Logger $ logger ,
116
- \ Magento \ Customer \ Api \ AddressRepositoryInterface $ addressRepository ,
117
- \ Magento \ Framework \ App \ Config \ ScopeConfigInterface $ scopeConfig ,
118
- \ Magento \ Quote \ Model \ Quote \ TotalsCollector $ totalsCollector ,
124
+ AddressRepositoryInterface $ addressRepository ,
125
+ ScopeConfigInterface $ scopeConfig ,
126
+ TotalsCollector $ totalsCollector ,
119
127
CartExtensionFactory $ cartExtensionFactory = null ,
120
128
ShippingAssignmentFactory $ shippingAssignmentFactory = null ,
121
129
ShippingFactory $ shippingFactory = null
@@ -141,24 +149,23 @@ public function __construct(
141
149
* Save address information.
142
150
*
143
151
* @param int $cartId
144
- * @param \Magento\Checkout\Api\Data\ ShippingInformationInterface $addressInformation
145
- * @return \Magento\Checkout\Api\Data\ PaymentDetailsInterface
152
+ * @param ShippingInformationInterface $addressInformation
153
+ * @return PaymentDetailsInterface
146
154
* @throws InputException
147
155
* @throws NoSuchEntityException
148
156
* @throws StateException
149
157
*/
150
158
public function saveAddressInformation (
151
159
$ cartId ,
152
- \ Magento \ Checkout \ Api \ Data \ ShippingInformationInterface $ addressInformation
153
- ) {
154
- /** @var \Magento\Quote\Model\ Quote $quote */
160
+ ShippingInformationInterface $ addressInformation
161
+ ): PaymentDetailsInterface {
162
+ /** @var Quote $quote */
155
163
$ quote = $ this ->quoteRepository ->getActive ($ cartId );
156
164
$ this ->validateQuote ($ quote );
157
165
158
166
$ address = $ addressInformation ->getShippingAddress ();
159
- if (!$ address || !$ address ->getCountryId ()) {
160
- throw new StateException (__ ('The shipping address is missing. Set the address and try again. ' ));
161
- }
167
+ $ this ->validateAddress ($ address );
168
+
162
169
if (!$ address ->getCustomerAddressId ()) {
163
170
$ address ->setCustomerAddressId (null );
164
171
}
@@ -182,6 +189,14 @@ public function saveAddressInformation(
182
189
$ quote ->setIsMultiShipping (false );
183
190
184
191
$ this ->quoteRepository ->save ($ quote );
192
+ } catch (LocalizedException $ e ) {
193
+ $ this ->logger ->critical ($ e );
194
+ throw new InputException (
195
+ __ (
196
+ 'The shipping information was unable to be saved. Error: "%message" ' ,
197
+ ['message ' => $ e ->getMessage ()]
198
+ )
199
+ );
185
200
} catch (\Exception $ e ) {
186
201
$ this ->logger ->critical ($ e );
187
202
throw new InputException (
@@ -199,26 +214,39 @@ public function saveAddressInformation(
199
214
);
200
215
}
201
216
202
- /** @var \Magento\Checkout\Api\Data\ PaymentDetailsInterface $paymentDetails */
217
+ /** @var PaymentDetailsInterface $paymentDetails */
203
218
$ paymentDetails = $ this ->paymentDetailsFactory ->create ();
204
219
$ paymentDetails ->setPaymentMethods ($ this ->paymentMethodManagement ->getList ($ cartId ));
205
220
$ paymentDetails ->setTotals ($ this ->cartTotalsRepository ->get ($ cartId ));
206
221
return $ paymentDetails ;
207
222
}
208
223
224
+ /**
225
+ * Validate shipping address
226
+ *
227
+ * @param AddressInterface|null $address
228
+ * @return void
229
+ * @throws StateException
230
+ */
231
+ private function validateAddress (?AddressInterface $ address ): void
232
+ {
233
+ if (!$ address || !$ address ->getCountryId ()) {
234
+ throw new StateException (__ ('The shipping address is missing. Set the address and try again. ' ));
235
+ }
236
+ }
237
+
209
238
/**
210
239
* Validate quote
211
240
*
212
- * @param \Magento\Quote\Model\ Quote $quote
241
+ * @param Quote $quote
213
242
* @throws InputException
214
- * @throws NoSuchEntityException
215
243
* @return void
216
244
*/
217
- protected function validateQuote (\ Magento \ Quote \ Model \ Quote $ quote )
245
+ protected function validateQuote (Quote $ quote ): void
218
246
{
219
- if (0 == $ quote ->getItemsCount ()) {
247
+ if (! $ quote ->getItemsCount ()) {
220
248
throw new InputException (
221
- __ (" The shipping method can't be set for an empty cart. Add an item to cart and try again. " )
249
+ __ (' The shipping method can \ 't be set for an empty cart. Add an item to cart and try again. ' )
222
250
);
223
251
}
224
252
}
@@ -231,7 +259,7 @@ protected function validateQuote(\Magento\Quote\Model\Quote $quote)
231
259
* @param string $method
232
260
* @return CartInterface
233
261
*/
234
- private function prepareShippingAssignment (CartInterface $ quote , AddressInterface $ address , $ method )
262
+ private function prepareShippingAssignment (CartInterface $ quote , AddressInterface $ address , $ method ): CartInterface
235
263
{
236
264
$ cartExtension = $ quote ->getExtensionAttributes ();
237
265
if ($ cartExtension === null ) {
0 commit comments