Skip to content

Commit 42813cc

Browse files
committed
Extract carriers status and config to separate class
1 parent f26ae80 commit 42813cc

File tree

8 files changed

+371
-305
lines changed

8 files changed

+371
-305
lines changed

app/code/Magento/Shipping/Model/Carrier/AbstractCarrier.php

Lines changed: 75 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,20 @@
66

77
namespace Magento\Shipping\Model\Carrier;
88

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;
915
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;
1019
use Magento\Shipping\Model\Shipment\Request;
20+
use Magento\Store\Model\ScopeInterface;
21+
use Psr\Log\LoggerInterface;
22+
use SimpleXMLElement;
1123

1224
/**
1325
* Class AbstractCarrier
@@ -16,7 +28,7 @@
1628
* @api
1729
* @since 100.0.2
1830
*/
19-
abstract class AbstractCarrier extends \Magento\Framework\DataObject implements AbstractCarrierInterface
31+
abstract class AbstractCarrier extends DataObject implements AbstractCarrierInterface
2032
{
2133
public const DEBUG_KEYS_MASK = '****';
2234

@@ -72,36 +84,40 @@ abstract class AbstractCarrier extends \Magento\Framework\DataObject implements
7284
/**
7385
* Core store config
7486
*
75-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
87+
* @var ScopeConfigInterface
7688
*/
7789
protected $_scopeConfig;
7890

7991
/**
80-
* @var \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory
92+
* @var ErrorFactory
8193
*/
8294
protected $_rateErrorFactory;
8395

8496
/**
85-
* @var \Psr\Log\LoggerInterface
97+
* @var LoggerInterface
8698
*/
8799
protected $_logger;
88100

101+
protected CarrierStatus $carrierStatus;
102+
89103
/**
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
93107
* @param array $data
94108
*/
95109
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
100115
) {
101116
parent::__construct($data);
102117
$this->_scopeConfig = $scopeConfig;
103118
$this->_rateErrorFactory = $rateErrorFactory;
104119
$this->_logger = $logger;
120+
$this->carrierStatus = $carrierStatus ?? ObjectManager::getInstance()->get(CarrierStatus::class);
105121
}
106122

107123
/**
@@ -117,11 +133,7 @@ public function getConfigData($field)
117133
}
118134
$path = 'carriers/' . $this->_code . '/' . $field;
119135

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());
125137
}
126138

127139
/**
@@ -138,11 +150,7 @@ public function getConfigFlag($field)
138150
}
139151
$path = 'carriers/' . $this->_code . '/' . $field;
140152

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());
146154
}
147155

148156
/**
@@ -151,12 +159,12 @@ public function getConfigFlag($field)
151159
* Implementation must be in overridden method
152160
*
153161
* @param Request $request
154-
* @return \Magento\Framework\DataObject
162+
* @return DataObject
155163
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
156164
*/
157165
public function requestToShipment($request)
158166
{
159-
return new \Magento\Framework\DataObject();
167+
return new DataObject();
160168
}
161169

162170
/**
@@ -165,35 +173,35 @@ public function requestToShipment($request)
165173
* Implementation must be in overridden method
166174
*
167175
* @param Request $request
168-
* @return \Magento\Framework\DataObject
176+
* @return DataObject
169177
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
170178
*/
171179
public function returnOfShipment($request)
172180
{
173-
return new \Magento\Framework\DataObject();
181+
return new DataObject();
174182
}
175183

176184
/**
177185
* Return container types of carrier
178186
*
179-
* @param \Magento\Framework\DataObject|null $params
187+
* @param DataObject|null $params
180188
* @return array
181189
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
182190
*/
183-
public function getContainerTypes(\Magento\Framework\DataObject $params = null)
191+
public function getContainerTypes(DataObject $params = null)
184192
{
185193
return [];
186194
}
187195

188196
/**
189197
* Get allowed containers of carrier
190198
*
191-
* @param \Magento\Framework\DataObject|null $params
199+
* @param DataObject|null $params
192200
* @return array|bool
193201
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
194202
* @SuppressWarnings(PHPMD.NPathComplexity)
195203
*/
196-
protected function _getAllowedContainers(\Magento\Framework\DataObject $params = null)
204+
protected function _getAllowedContainers(DataObject $params = null)
197205
{
198206
$containersAll = $this->getContainerTypesAll();
199207
if (empty($containersAll)) {
@@ -215,14 +223,10 @@ protected function _getAllowedContainers(\Magento\Framework\DataObject $params =
215223
return $containersAll;
216224
}
217225

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';
220228
} 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;
226230
}
227231

228232
foreach ($containersFilter as $dataItem) {
@@ -253,23 +257,23 @@ public function getCustomizableContainerTypes()
253257
/**
254258
* Return delivery confirmation types of carrier
255259
*
256-
* @param \Magento\Framework\DataObject|null $params
260+
* @param DataObject|null $params
257261
* @return array
258262
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
259263
*/
260-
public function getDeliveryConfirmationTypes(\Magento\Framework\DataObject $params = null)
264+
public function getDeliveryConfirmationTypes(DataObject $params = null)
261265
{
262266
return [];
263267
}
264268

265269
/**
266270
* Validate request for available ship countries.
267271
*
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
270274
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
271275
*/
272-
public function checkAvailableShipCountries(\Magento\Framework\DataObject $request)
276+
public function checkAvailableShipCountries(DataObject $request)
273277
{
274278
$speCountriesAllow = $this->getConfigData('sallowspecific');
275279
/*
@@ -283,30 +287,29 @@ public function checkAvailableShipCountries(\Magento\Framework\DataObject $reque
283287
}
284288
if ($availableCountries && in_array($request->getDestCountryId(), $availableCountries)) {
285289
return $this;
286-
} elseif ($showMethod && (!$availableCountries || $availableCountries && !in_array(
290+
}
291+
if ($showMethod && (!$availableCountries || $availableCountries && !in_array(
287292
$request->getDestCountryId(),
288293
$availableCountries
289-
))
290-
) {
294+
))) {
291295
/** @var Error $error */
292296
$error = $this->_rateErrorFactory->create();
293297
$error->setCarrier($this->_code);
294298
$error->setCarrierTitle($this->getConfigData('title'));
295299
$errorMsg = $this->getConfigData('specificerrmsg');
296300
$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.')
300303
);
301304

302305
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;
309306
}
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;
310313
}
311314

312315
return $this;
@@ -315,25 +318,25 @@ public function checkAvailableShipCountries(\Magento\Framework\DataObject $reque
315318
/**
316319
* Processing additional validation to check is carrier applicable.
317320
*
318-
* @param \Magento\Framework\DataObject $request
319-
* @return $this|bool|\Magento\Framework\DataObject
321+
* @param DataObject $request
322+
* @return $this|bool|DataObject
320323
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
321324
* @since 100.2.6
322325
*/
323-
public function processAdditionalValidation(\Magento\Framework\DataObject $request)
326+
public function processAdditionalValidation(DataObject $request)
324327
{
325328
return $this;
326329
}
327330

328331
/**
329332
* Processing additional validation to check is carrier applicable.
330333
*
331-
* @param \Magento\Framework\DataObject $request
332-
* @return $this|bool|\Magento\Framework\DataObject
334+
* @param DataObject $request
335+
* @return $this|bool|DataObject
333336
* @deprecated 100.2.6
334337
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
335338
*/
336-
public function proccessAdditionalValidation(\Magento\Framework\DataObject $request)
339+
public function proccessAdditionalValidation(DataObject $request)
337340
{
338341
return $this->processAdditionalValidation($request);
339342
}
@@ -345,9 +348,7 @@ public function proccessAdditionalValidation(\Magento\Framework\DataObject $requ
345348
*/
346349
public function isActive()
347350
{
348-
$active = $this->getConfigData('active');
349-
350-
return $active == 1 || $active == 'true';
351+
return $this->carrierStatus->isEnabled($this->_code);
351352
}
352353

353354
/**
@@ -393,7 +394,7 @@ public function getSortOrder()
393394
/**
394395
* Check if the request has free shipping weight
395396
*
396-
* @param \Magento\Quote\Model\Quote\Address\RateRequest $request
397+
* @param RateRequest $request
397398
* @return bool
398399
*/
399400
private function hasFreeMethodWeight($request): bool
@@ -410,7 +411,7 @@ private function hasFreeMethodWeight($request): bool
410411
/**
411412
* Allows free shipping when all product items have free shipping.
412413
*
413-
* @param \Magento\Quote\Model\Quote\Address\RateRequest $request
414+
* @param RateRequest $request
414415
* @return void
415416
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
416417
* @SuppressWarnings(PHPMD.NPathComplexity)
@@ -447,12 +448,12 @@ protected function _updateFreeMethodQuote($request)
447448
// phpstan:ignore
448449
$result = $this->_getQuotes();
449450
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) {
451452
$price = $rates[0]->getPrice();
452453
}
453454
if (count($rates) > 1) {
454455
foreach ($rates as $rate) {
455-
if ($rate instanceof \Magento\Quote\Model\Quote\Address\RateResult\Method &&
456+
if ($rate instanceof Method &&
456457
$rate->getMethod() == $freeMethod
457458
) {
458459
$price = $rate->getPrice();
@@ -640,11 +641,11 @@ public function getCarrierCode()
640641
/**
641642
* Return content types of package
642643
*
643-
* @param \Magento\Framework\DataObject $params
644+
* @param DataObject $params
644645
* @return array
645646
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
646647
*/
647-
public function getContentTypes(\Magento\Framework\DataObject $params)
648+
public function getContentTypes(DataObject $params)
648649
{
649650
return [];
650651
}
@@ -670,10 +671,10 @@ public function getContentTypes(\Magento\Framework\DataObject $params)
670671
protected function filterDebugData($data)
671672
{
672673
try {
673-
$xml = new \SimpleXMLElement($data);
674+
$xml = new SimpleXMLElement($data);
674675
$this->filterXmlData($xml);
675676
$data = $xml->asXML();
676-
} catch (\Exception $e) {
677+
} catch (Exception $e) {
677678
$this->_logger->critical($e);
678679
}
679680
return $data;
@@ -682,16 +683,16 @@ protected function filterDebugData($data)
682683
/**
683684
* Recursive replace sensitive xml nodes values by specified mask.
684685
*
685-
* @param \SimpleXMLElement $xml
686+
* @param SimpleXMLElement $xml
686687
* @return void
687688
*/
688-
private function filterXmlData(\SimpleXMLElement $xml)
689+
private function filterXmlData(SimpleXMLElement $xml): void
689690
{
690-
/** @var \SimpleXMLElement $child */
691+
/** @var SimpleXMLElement $child */
691692
foreach ($xml->children() as $child) {
692693
if ($child->count()) {
693694
$this->filterXmlData($child);
694-
} elseif (in_array((string) $child->getName(), $this->_debugReplacePrivateDataKeys)) {
695+
} elseif (in_array($child->getName(), $this->_debugReplacePrivateDataKeys, true)) {
695696
$child[0] = self::DEBUG_KEYS_MASK;
696697
}
697698
}

0 commit comments

Comments
 (0)