Skip to content

Commit 9a1320b

Browse files
fix static, unit test
1 parent 162da03 commit 9a1320b

File tree

2 files changed

+438
-177
lines changed

2 files changed

+438
-177
lines changed

app/code/Magento/Checkout/Model/ShippingInformationManagement.php

Lines changed: 77 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,38 @@
66

77
namespace Magento\Checkout\Model;
88

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;
915
use Magento\Framework\Exception\InputException;
10-
use Magento\Framework\Exception\StateException;
16+
use Magento\Framework\Exception\LocalizedException;
1117
use Magento\Framework\Exception\NoSuchEntityException;
18+
use Magento\Framework\Exception\StateException;
19+
use Magento\Quote\Api\CartRepositoryInterface;
20+
use Magento\Quote\Api\CartTotalRepositoryInterface;
1221
use Magento\Quote\Api\Data\AddressInterface;
22+
use Magento\Quote\Api\Data\CartExtensionFactory;
1323
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;
1527
use Magento\Quote\Model\QuoteAddressValidator;
16-
use Magento\Quote\Api\Data\CartExtensionFactory;
1728
use Magento\Quote\Model\ShippingAssignmentFactory;
1829
use Magento\Quote\Model\ShippingFactory;
19-
use Magento\Framework\App\ObjectManager;
30+
use Psr\Log\LoggerInterface as Logger;
2031

2132
/**
22-
* Class ShippingInformationManagement
33+
* @inheritdoc
2334
*
24-
* @package Magento\Checkout\Model
2535
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2636
*/
27-
class ShippingInformationManagement implements \Magento\Checkout\Api\ShippingInformationManagementInterface
37+
class ShippingInformationManagement implements ShippingInformationManagementInterface
2838
{
2939
/**
30-
* @var \Magento\Quote\Api\PaymentMethodManagementInterface
40+
* @var PaymentMethodManagementInterface
3141
*/
3242
protected $paymentMethodManagement;
3343

@@ -37,12 +47,12 @@ class ShippingInformationManagement implements \Magento\Checkout\Api\ShippingInf
3747
protected $paymentDetailsFactory;
3848

3949
/**
40-
* @var \Magento\Quote\Api\CartTotalRepositoryInterface
50+
* @var CartTotalRepositoryInterface
4151
*/
4252
protected $cartTotalsRepository;
4353

4454
/**
45-
* @var \Magento\Quote\Api\CartRepositoryInterface
55+
* @var CartRepositoryInterface
4656
*/
4757
protected $quoteRepository;
4858

@@ -57,65 +67,63 @@ class ShippingInformationManagement implements \Magento\Checkout\Api\ShippingInf
5767
protected $addressValidator;
5868

5969
/**
60-
* @var \Magento\Customer\Api\AddressRepositoryInterface
70+
* @var AddressRepositoryInterface
6171
* @deprecated 100.2.0
6272
*/
6373
protected $addressRepository;
6474

6575
/**
66-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
76+
* @var ScopeConfigInterface
6777
* @deprecated 100.2.0
6878
*/
6979
protected $scopeConfig;
7080

7181
/**
72-
* @var \Magento\Quote\Model\Quote\TotalsCollector
82+
* @var TotalsCollector
7383
* @deprecated 100.2.0
7484
*/
7585
protected $totalsCollector;
7686

7787
/**
78-
* @var \Magento\Quote\Api\Data\CartExtensionFactory
88+
* @var CartExtensionFactory
7989
*/
8090
private $cartExtensionFactory;
8191

8292
/**
83-
* @var \Magento\Quote\Model\ShippingAssignmentFactory
93+
* @var ShippingAssignmentFactory
8494
*/
8595
protected $shippingAssignmentFactory;
8696

8797
/**
88-
* @var \Magento\Quote\Model\ShippingFactory
98+
* @var ShippingFactory
8999
*/
90100
private $shippingFactory;
91101

92102
/**
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
100108
* @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
104112
* @param CartExtensionFactory|null $cartExtensionFactory
105113
* @param ShippingAssignmentFactory|null $shippingAssignmentFactory
106114
* @param ShippingFactory|null $shippingFactory
107115
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
108116
*/
109117
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,
114122
QuoteAddressValidator $addressValidator,
115123
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,
119127
CartExtensionFactory $cartExtensionFactory = null,
120128
ShippingAssignmentFactory $shippingAssignmentFactory = null,
121129
ShippingFactory $shippingFactory = null
@@ -141,24 +149,23 @@ public function __construct(
141149
* Save address information.
142150
*
143151
* @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
146154
* @throws InputException
147155
* @throws NoSuchEntityException
148156
* @throws StateException
149157
*/
150158
public function saveAddressInformation(
151159
$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 */
155163
$quote = $this->quoteRepository->getActive($cartId);
156164
$this->validateQuote($quote);
157165

158166
$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+
162169
if (!$address->getCustomerAddressId()) {
163170
$address->setCustomerAddressId(null);
164171
}
@@ -182,10 +189,18 @@ public function saveAddressInformation(
182189
$quote->setIsMultiShipping(false);
183190

184191
$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+
);
185200
} catch (\Exception $e) {
186201
$this->logger->critical($e);
187202
throw new InputException(
188-
__('The shipping information was unable to be saved. Error: "%1"', $e->getMessage())
203+
__('The shipping information was unable to be saved. Verify the input data and try again.')
189204
);
190205
}
191206

@@ -199,26 +214,39 @@ public function saveAddressInformation(
199214
);
200215
}
201216

202-
/** @var \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails */
217+
/** @var PaymentDetailsInterface $paymentDetails */
203218
$paymentDetails = $this->paymentDetailsFactory->create();
204219
$paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId));
205220
$paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));
206221
return $paymentDetails;
207222
}
208223

224+
/**
225+
* Validate shipping address
226+
*
227+
* @param AddressInterface $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+
209238
/**
210239
* Validate quote
211240
*
212-
* @param \Magento\Quote\Model\Quote $quote
241+
* @param Quote $quote
213242
* @throws InputException
214-
* @throws NoSuchEntityException
215243
* @return void
216244
*/
217-
protected function validateQuote(\Magento\Quote\Model\Quote $quote)
245+
protected function validateQuote(Quote $quote): void
218246
{
219-
if (0 == $quote->getItemsCount()) {
247+
if (!$quote->getItemsCount()) {
220248
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.')
222250
);
223251
}
224252
}
@@ -231,7 +259,7 @@ protected function validateQuote(\Magento\Quote\Model\Quote $quote)
231259
* @param string $method
232260
* @return CartInterface
233261
*/
234-
private function prepareShippingAssignment(CartInterface $quote, AddressInterface $address, $method)
262+
private function prepareShippingAssignment(CartInterface $quote, AddressInterface $address, $method): CartInterface
235263
{
236264
$cartExtension = $quote->getExtensionAttributes();
237265
if ($cartExtension === null) {

0 commit comments

Comments
 (0)