6
6
7
7
namespace Magento \Shipping \Model \Carrier ;
8
8
9
+ use Exception ;
10
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
11
+ use Magento \Framework \App \ObjectManager ;
12
+ use Magento \Framework \DataObject ;
13
+ use Magento \Framework \Model \AbstractModel ;
14
+ use Magento \Quote \Model \Quote \Address \RateRequest ;
9
15
use Magento \Quote \Model \Quote \Address \RateResult \Error ;
16
+ use Magento \Quote \Model \Quote \Address \RateResult \ErrorFactory ;
17
+ use Magento \Quote \Model \Quote \Address \RateResult \Method ;
18
+ use Magento \Shipping \Model \Config \CarrierStatus ;
10
19
use Magento \Shipping \Model \Shipment \Request ;
20
+ use Magento \Store \Model \ScopeInterface ;
21
+ use Psr \Log \LoggerInterface ;
22
+ use SimpleXMLElement ;
11
23
12
24
/**
13
25
* Class AbstractCarrier
16
28
* @api
17
29
* @since 100.0.2
18
30
*/
19
- abstract class AbstractCarrier extends \ Magento \ Framework \ DataObject implements AbstractCarrierInterface
31
+ abstract class AbstractCarrier extends DataObject implements AbstractCarrierInterface
20
32
{
21
33
public const DEBUG_KEYS_MASK = '**** ' ;
22
34
@@ -72,36 +84,40 @@ abstract class AbstractCarrier extends \Magento\Framework\DataObject implements
72
84
/**
73
85
* Core store config
74
86
*
75
- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
87
+ * @var ScopeConfigInterface
76
88
*/
77
89
protected $ _scopeConfig ;
78
90
79
91
/**
80
- * @var \Magento\Quote\Model\Quote\Address\RateResult\ ErrorFactory
92
+ * @var ErrorFactory
81
93
*/
82
94
protected $ _rateErrorFactory ;
83
95
84
96
/**
85
- * @var \Psr\Log\ LoggerInterface
97
+ * @var LoggerInterface
86
98
*/
87
99
protected $ _logger ;
88
100
101
+ protected CarrierStatus $ carrierStatus ;
102
+
89
103
/**
90
- * @param \Magento\Framework\App\Config\ ScopeConfigInterface $scopeConfig
91
- * @param \Magento\Quote\Model\Quote\Address\RateResult\ ErrorFactory $rateErrorFactory
92
- * @param \Psr\Log\ LoggerInterface $logger
104
+ * @param ScopeConfigInterface $scopeConfig
105
+ * @param ErrorFactory $rateErrorFactory
106
+ * @param LoggerInterface $logger
93
107
* @param array $data
94
108
*/
95
109
public function __construct (
96
- \Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig ,
97
- \Magento \Quote \Model \Quote \Address \RateResult \ErrorFactory $ rateErrorFactory ,
98
- \Psr \Log \LoggerInterface $ logger ,
99
- array $ data = []
110
+ ScopeConfigInterface $ scopeConfig ,
111
+ ErrorFactory $ rateErrorFactory ,
112
+ LoggerInterface $ logger ,
113
+ array $ data = [],
114
+ CarrierStatus $ carrierStatus = null
100
115
) {
101
116
parent ::__construct ($ data );
102
117
$ this ->_scopeConfig = $ scopeConfig ;
103
118
$ this ->_rateErrorFactory = $ rateErrorFactory ;
104
119
$ this ->_logger = $ logger ;
120
+ $ this ->carrierStatus = $ carrierStatus ?? ObjectManager::getInstance ()->get (CarrierStatus::class);
105
121
}
106
122
107
123
/**
@@ -117,11 +133,7 @@ public function getConfigData($field)
117
133
}
118
134
$ path = 'carriers/ ' . $ this ->_code . '/ ' . $ field ;
119
135
120
- return $ this ->_scopeConfig ->getValue (
121
- $ path ,
122
- \Magento \Store \Model \ScopeInterface::SCOPE_STORE ,
123
- $ this ->getStore ()
124
- );
136
+ return $ this ->_scopeConfig ->getValue ($ path , ScopeInterface::SCOPE_STORE , $ this ->getStore ());
125
137
}
126
138
127
139
/**
@@ -138,11 +150,7 @@ public function getConfigFlag($field)
138
150
}
139
151
$ path = 'carriers/ ' . $ this ->_code . '/ ' . $ field ;
140
152
141
- return $ this ->_scopeConfig ->isSetFlag (
142
- $ path ,
143
- \Magento \Store \Model \ScopeInterface::SCOPE_STORE ,
144
- $ this ->getStore ()
145
- );
153
+ return $ this ->_scopeConfig ->isSetFlag ($ path , ScopeInterface::SCOPE_STORE , $ this ->getStore ());
146
154
}
147
155
148
156
/**
@@ -151,12 +159,12 @@ public function getConfigFlag($field)
151
159
* Implementation must be in overridden method
152
160
*
153
161
* @param Request $request
154
- * @return \Magento\Framework\ DataObject
162
+ * @return DataObject
155
163
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
156
164
*/
157
165
public function requestToShipment ($ request )
158
166
{
159
- return new \ Magento \ Framework \ DataObject ();
167
+ return new DataObject ();
160
168
}
161
169
162
170
/**
@@ -165,35 +173,35 @@ public function requestToShipment($request)
165
173
* Implementation must be in overridden method
166
174
*
167
175
* @param Request $request
168
- * @return \Magento\Framework\ DataObject
176
+ * @return DataObject
169
177
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
170
178
*/
171
179
public function returnOfShipment ($ request )
172
180
{
173
- return new \ Magento \ Framework \ DataObject ();
181
+ return new DataObject ();
174
182
}
175
183
176
184
/**
177
185
* Return container types of carrier
178
186
*
179
- * @param \Magento\Framework\ DataObject|null $params
187
+ * @param DataObject|null $params
180
188
* @return array
181
189
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
182
190
*/
183
- public function getContainerTypes (\ Magento \ Framework \ DataObject $ params = null )
191
+ public function getContainerTypes (DataObject $ params = null )
184
192
{
185
193
return [];
186
194
}
187
195
188
196
/**
189
197
* Get allowed containers of carrier
190
198
*
191
- * @param \Magento\Framework\ DataObject|null $params
199
+ * @param DataObject|null $params
192
200
* @return array|bool
193
201
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
194
202
* @SuppressWarnings(PHPMD.NPathComplexity)
195
203
*/
196
- protected function _getAllowedContainers (\ Magento \ Framework \ DataObject $ params = null )
204
+ protected function _getAllowedContainers (DataObject $ params = null )
197
205
{
198
206
$ containersAll = $ this ->getContainerTypesAll ();
199
207
if (empty ($ containersAll )) {
@@ -215,14 +223,10 @@ protected function _getAllowedContainers(\Magento\Framework\DataObject $params =
215
223
return $ containersAll ;
216
224
}
217
225
218
- if ($ countryShipper == self :: USA_COUNTRY_ID && $ countryRecipient = = self ::USA_COUNTRY_ID ) {
219
- $ direction = 'within_us ' ;
226
+ if ($ countryShipper === self ::USA_COUNTRY_ID ) {
227
+ $ direction = $ countryRecipient === self :: USA_COUNTRY_ID ? 'within_us ' : ' from_us ' ;
220
228
} else {
221
- if ($ countryShipper == self ::USA_COUNTRY_ID && $ countryRecipient != self ::USA_COUNTRY_ID ) {
222
- $ direction = 'from_us ' ;
223
- } else {
224
- return $ containersAll ;
225
- }
229
+ return $ containersAll ;
226
230
}
227
231
228
232
foreach ($ containersFilter as $ dataItem ) {
@@ -253,23 +257,23 @@ public function getCustomizableContainerTypes()
253
257
/**
254
258
* Return delivery confirmation types of carrier
255
259
*
256
- * @param \Magento\Framework\ DataObject|null $params
260
+ * @param DataObject|null $params
257
261
* @return array
258
262
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
259
263
*/
260
- public function getDeliveryConfirmationTypes (\ Magento \ Framework \ DataObject $ params = null )
264
+ public function getDeliveryConfirmationTypes (DataObject $ params = null )
261
265
{
262
266
return [];
263
267
}
264
268
265
269
/**
266
270
* Validate request for available ship countries.
267
271
*
268
- * @param \Magento\Framework\ DataObject $request
269
- * @return $this|bool|false|\Magento\Framework\Model\ AbstractModel
272
+ * @param DataObject $request
273
+ * @return $this|bool|false|AbstractModel
270
274
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
271
275
*/
272
- public function checkAvailableShipCountries (\ Magento \ Framework \ DataObject $ request )
276
+ public function checkAvailableShipCountries (DataObject $ request )
273
277
{
274
278
$ speCountriesAllow = $ this ->getConfigData ('sallowspecific ' );
275
279
/*
@@ -283,30 +287,29 @@ public function checkAvailableShipCountries(\Magento\Framework\DataObject $reque
283
287
}
284
288
if ($ availableCountries && in_array ($ request ->getDestCountryId (), $ availableCountries )) {
285
289
return $ this ;
286
- } elseif ($ showMethod && (!$ availableCountries || $ availableCountries && !in_array (
290
+ }
291
+ if ($ showMethod && (!$ availableCountries || $ availableCountries && !in_array (
287
292
$ request ->getDestCountryId (),
288
293
$ availableCountries
289
- ))
290
- ) {
294
+ ))) {
291
295
/** @var Error $error */
292
296
$ error = $ this ->_rateErrorFactory ->create ();
293
297
$ error ->setCarrier ($ this ->_code );
294
298
$ error ->setCarrierTitle ($ this ->getConfigData ('title ' ));
295
299
$ errorMsg = $ this ->getConfigData ('specificerrmsg ' );
296
300
$ error ->setErrorMessage (
297
- $ errorMsg ? $ errorMsg : __ (
298
- 'Sorry, but we can \'t deliver to the destination country with this shipping module. '
299
- )
301
+ $ errorMsg
302
+ ?: __ ('Sorry, but we can \'t deliver to the destination country with this shipping module. ' )
300
303
);
301
304
302
305
return $ error ;
303
- } else {
304
- /*
305
- * The admin set not to show the shipping module if the delivery country
306
- * is not within specific countries
307
- */
308
- return false ;
309
306
}
307
+
308
+ /*
309
+ * The admin set not to show the shipping module if the delivery country
310
+ * is not within specific countries
311
+ */
312
+ return false ;
310
313
}
311
314
312
315
return $ this ;
@@ -315,25 +318,25 @@ public function checkAvailableShipCountries(\Magento\Framework\DataObject $reque
315
318
/**
316
319
* Processing additional validation to check is carrier applicable.
317
320
*
318
- * @param \Magento\Framework\ DataObject $request
319
- * @return $this|bool|\Magento\Framework\ DataObject
321
+ * @param DataObject $request
322
+ * @return $this|bool|DataObject
320
323
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
321
324
* @since 100.2.6
322
325
*/
323
- public function processAdditionalValidation (\ Magento \ Framework \ DataObject $ request )
326
+ public function processAdditionalValidation (DataObject $ request )
324
327
{
325
328
return $ this ;
326
329
}
327
330
328
331
/**
329
332
* Processing additional validation to check is carrier applicable.
330
333
*
331
- * @param \Magento\Framework\ DataObject $request
332
- * @return $this|bool|\Magento\Framework\ DataObject
334
+ * @param DataObject $request
335
+ * @return $this|bool|DataObject
333
336
* @deprecated 100.2.6
334
337
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
335
338
*/
336
- public function proccessAdditionalValidation (\ Magento \ Framework \ DataObject $ request )
339
+ public function proccessAdditionalValidation (DataObject $ request )
337
340
{
338
341
return $ this ->processAdditionalValidation ($ request );
339
342
}
@@ -345,9 +348,7 @@ public function proccessAdditionalValidation(\Magento\Framework\DataObject $requ
345
348
*/
346
349
public function isActive ()
347
350
{
348
- $ active = $ this ->getConfigData ('active ' );
349
-
350
- return $ active == 1 || $ active == 'true ' ;
351
+ return $ this ->carrierStatus ->isEnabled ($ this ->_code );
351
352
}
352
353
353
354
/**
@@ -393,7 +394,7 @@ public function getSortOrder()
393
394
/**
394
395
* Check if the request has free shipping weight
395
396
*
396
- * @param \Magento\Quote\Model\Quote\Address\ RateRequest $request
397
+ * @param RateRequest $request
397
398
* @return bool
398
399
*/
399
400
private function hasFreeMethodWeight ($ request ): bool
@@ -410,7 +411,7 @@ private function hasFreeMethodWeight($request): bool
410
411
/**
411
412
* Allows free shipping when all product items have free shipping.
412
413
*
413
- * @param \Magento\Quote\Model\Quote\Address\ RateRequest $request
414
+ * @param RateRequest $request
414
415
* @return void
415
416
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
416
417
* @SuppressWarnings(PHPMD.NPathComplexity)
@@ -447,12 +448,12 @@ protected function _updateFreeMethodQuote($request)
447
448
// phpstan:ignore
448
449
$ result = $ this ->_getQuotes ();
449
450
if ($ result && ($ rates = $ result ->getAllRates ()) && count ($ rates ) > 0 ) {
450
- if (count ($ rates ) == 1 && $ rates [0 ] instanceof \ Magento \ Quote \ Model \ Quote \ Address \ RateResult \ Method) {
451
+ if (count ($ rates ) == 1 && $ rates [0 ] instanceof Method) {
451
452
$ price = $ rates [0 ]->getPrice ();
452
453
}
453
454
if (count ($ rates ) > 1 ) {
454
455
foreach ($ rates as $ rate ) {
455
- if ($ rate instanceof \ Magento \ Quote \ Model \ Quote \ Address \ RateResult \ Method &&
456
+ if ($ rate instanceof Method &&
456
457
$ rate ->getMethod () == $ freeMethod
457
458
) {
458
459
$ price = $ rate ->getPrice ();
@@ -640,11 +641,11 @@ public function getCarrierCode()
640
641
/**
641
642
* Return content types of package
642
643
*
643
- * @param \Magento\Framework\ DataObject $params
644
+ * @param DataObject $params
644
645
* @return array
645
646
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
646
647
*/
647
- public function getContentTypes (\ Magento \ Framework \ DataObject $ params )
648
+ public function getContentTypes (DataObject $ params )
648
649
{
649
650
return [];
650
651
}
@@ -670,10 +671,10 @@ public function getContentTypes(\Magento\Framework\DataObject $params)
670
671
protected function filterDebugData ($ data )
671
672
{
672
673
try {
673
- $ xml = new \ SimpleXMLElement ($ data );
674
+ $ xml = new SimpleXMLElement ($ data );
674
675
$ this ->filterXmlData ($ xml );
675
676
$ data = $ xml ->asXML ();
676
- } catch (\ Exception $ e ) {
677
+ } catch (Exception $ e ) {
677
678
$ this ->_logger ->critical ($ e );
678
679
}
679
680
return $ data ;
@@ -682,16 +683,16 @@ protected function filterDebugData($data)
682
683
/**
683
684
* Recursive replace sensitive xml nodes values by specified mask.
684
685
*
685
- * @param \ SimpleXMLElement $xml
686
+ * @param SimpleXMLElement $xml
686
687
* @return void
687
688
*/
688
- private function filterXmlData (\ SimpleXMLElement $ xml )
689
+ private function filterXmlData (SimpleXMLElement $ xml ): void
689
690
{
690
- /** @var \ SimpleXMLElement $child */
691
+ /** @var SimpleXMLElement $child */
691
692
foreach ($ xml ->children () as $ child ) {
692
693
if ($ child ->count ()) {
693
694
$ this ->filterXmlData ($ child );
694
- } elseif (in_array (( string ) $ child ->getName (), $ this ->_debugReplacePrivateDataKeys )) {
695
+ } elseif (in_array ($ child ->getName (), $ this ->_debugReplacePrivateDataKeys , true )) {
695
696
$ child [0 ] = self ::DEBUG_KEYS_MASK ;
696
697
}
697
698
}
0 commit comments