diff --git a/app/code/Magento/Shipping/Model/Carrier/AbstractCarrier.php b/app/code/Magento/Shipping/Model/Carrier/AbstractCarrier.php index d8a16023702d4..205453b8db3f6 100644 --- a/app/code/Magento/Shipping/Model/Carrier/AbstractCarrier.php +++ b/app/code/Magento/Shipping/Model/Carrier/AbstractCarrier.php @@ -6,8 +6,20 @@ namespace Magento\Shipping\Model\Carrier; +use Exception; +use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\DataObject; +use Magento\Framework\Model\AbstractModel; +use Magento\Quote\Model\Quote\Address\RateRequest; use Magento\Quote\Model\Quote\Address\RateResult\Error; +use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory; +use Magento\Quote\Model\Quote\Address\RateResult\Method; +use Magento\Shipping\Model\Config\CarrierStatus; use Magento\Shipping\Model\Shipment\Request; +use Magento\Store\Model\ScopeInterface; +use Psr\Log\LoggerInterface; +use SimpleXMLElement; /** * Class AbstractCarrier @@ -16,7 +28,7 @@ * @api * @since 100.0.2 */ -abstract class AbstractCarrier extends \Magento\Framework\DataObject implements AbstractCarrierInterface +abstract class AbstractCarrier extends DataObject implements AbstractCarrierInterface { public const DEBUG_KEYS_MASK = '****'; @@ -72,36 +84,40 @@ abstract class AbstractCarrier extends \Magento\Framework\DataObject implements /** * Core store config * - * @var \Magento\Framework\App\Config\ScopeConfigInterface + * @var ScopeConfigInterface */ protected $_scopeConfig; /** - * @var \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory + * @var ErrorFactory */ protected $_rateErrorFactory; /** - * @var \Psr\Log\LoggerInterface + * @var LoggerInterface */ protected $_logger; + protected CarrierStatus $carrierStatus; + /** - * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig - * @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory - * @param \Psr\Log\LoggerInterface $logger + * @param ScopeConfigInterface $scopeConfig + * @param ErrorFactory $rateErrorFactory + * @param LoggerInterface $logger * @param array $data */ public function __construct( - \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, - \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory, - \Psr\Log\LoggerInterface $logger, - array $data = [] + ScopeConfigInterface $scopeConfig, + ErrorFactory $rateErrorFactory, + LoggerInterface $logger, + array $data = [], + CarrierStatus $carrierStatus = null ) { parent::__construct($data); $this->_scopeConfig = $scopeConfig; $this->_rateErrorFactory = $rateErrorFactory; $this->_logger = $logger; + $this->carrierStatus = $carrierStatus ?? ObjectManager::getInstance()->get(CarrierStatus::class); } /** @@ -117,11 +133,7 @@ public function getConfigData($field) } $path = 'carriers/' . $this->_code . '/' . $field; - return $this->_scopeConfig->getValue( - $path, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, - $this->getStore() - ); + return $this->_scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $this->getStore()); } /** @@ -138,11 +150,7 @@ public function getConfigFlag($field) } $path = 'carriers/' . $this->_code . '/' . $field; - return $this->_scopeConfig->isSetFlag( - $path, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, - $this->getStore() - ); + return $this->_scopeConfig->isSetFlag($path, ScopeInterface::SCOPE_STORE, $this->getStore()); } /** @@ -151,12 +159,12 @@ public function getConfigFlag($field) * Implementation must be in overridden method * * @param Request $request - * @return \Magento\Framework\DataObject + * @return DataObject * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function requestToShipment($request) { - return new \Magento\Framework\DataObject(); + return new DataObject(); } /** @@ -165,22 +173,22 @@ public function requestToShipment($request) * Implementation must be in overridden method * * @param Request $request - * @return \Magento\Framework\DataObject + * @return DataObject * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function returnOfShipment($request) { - return new \Magento\Framework\DataObject(); + return new DataObject(); } /** * Return container types of carrier * - * @param \Magento\Framework\DataObject|null $params + * @param DataObject|null $params * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function getContainerTypes(\Magento\Framework\DataObject $params = null) + public function getContainerTypes(DataObject $params = null) { return []; } @@ -188,12 +196,12 @@ public function getContainerTypes(\Magento\Framework\DataObject $params = null) /** * Get allowed containers of carrier * - * @param \Magento\Framework\DataObject|null $params + * @param DataObject|null $params * @return array|bool * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ - protected function _getAllowedContainers(\Magento\Framework\DataObject $params = null) + protected function _getAllowedContainers(DataObject $params = null) { $containersAll = $this->getContainerTypesAll(); if (empty($containersAll)) { @@ -215,14 +223,10 @@ protected function _getAllowedContainers(\Magento\Framework\DataObject $params = return $containersAll; } - if ($countryShipper == self::USA_COUNTRY_ID && $countryRecipient == self::USA_COUNTRY_ID) { - $direction = 'within_us'; + if ($countryShipper === self::USA_COUNTRY_ID) { + $direction = $countryRecipient === self::USA_COUNTRY_ID ? 'within_us' : 'from_us'; } else { - if ($countryShipper == self::USA_COUNTRY_ID && $countryRecipient != self::USA_COUNTRY_ID) { - $direction = 'from_us'; - } else { - return $containersAll; - } + return $containersAll; } foreach ($containersFilter as $dataItem) { @@ -253,11 +257,11 @@ public function getCustomizableContainerTypes() /** * Return delivery confirmation types of carrier * - * @param \Magento\Framework\DataObject|null $params + * @param DataObject|null $params * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function getDeliveryConfirmationTypes(\Magento\Framework\DataObject $params = null) + public function getDeliveryConfirmationTypes(DataObject $params = null) { return []; } @@ -265,11 +269,11 @@ public function getDeliveryConfirmationTypes(\Magento\Framework\DataObject $para /** * Validate request for available ship countries. * - * @param \Magento\Framework\DataObject $request - * @return $this|bool|false|\Magento\Framework\Model\AbstractModel + * @param DataObject $request + * @return $this|bool|false|AbstractModel * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ - public function checkAvailableShipCountries(\Magento\Framework\DataObject $request) + public function checkAvailableShipCountries(DataObject $request) { $speCountriesAllow = $this->getConfigData('sallowspecific'); /* @@ -283,30 +287,29 @@ public function checkAvailableShipCountries(\Magento\Framework\DataObject $reque } if ($availableCountries && in_array($request->getDestCountryId(), $availableCountries)) { return $this; - } elseif ($showMethod && (!$availableCountries || $availableCountries && !in_array( + } + if ($showMethod && (!$availableCountries || $availableCountries && !in_array( $request->getDestCountryId(), $availableCountries - )) - ) { + ))) { /** @var Error $error */ $error = $this->_rateErrorFactory->create(); $error->setCarrier($this->_code); $error->setCarrierTitle($this->getConfigData('title')); $errorMsg = $this->getConfigData('specificerrmsg'); $error->setErrorMessage( - $errorMsg ? $errorMsg : __( - 'Sorry, but we can\'t deliver to the destination country with this shipping module.' - ) + $errorMsg + ?: __('Sorry, but we can\'t deliver to the destination country with this shipping module.') ); return $error; - } else { - /* - * The admin set not to show the shipping module if the delivery country - * is not within specific countries - */ - return false; } + + /* + * The admin set not to show the shipping module if the delivery country + * is not within specific countries + */ + return false; } return $this; @@ -315,12 +318,12 @@ public function checkAvailableShipCountries(\Magento\Framework\DataObject $reque /** * Processing additional validation to check is carrier applicable. * - * @param \Magento\Framework\DataObject $request - * @return $this|bool|\Magento\Framework\DataObject + * @param DataObject $request + * @return $this|bool|DataObject * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @since 100.2.6 */ - public function processAdditionalValidation(\Magento\Framework\DataObject $request) + public function processAdditionalValidation(DataObject $request) { return $this; } @@ -328,12 +331,12 @@ public function processAdditionalValidation(\Magento\Framework\DataObject $reque /** * Processing additional validation to check is carrier applicable. * - * @param \Magento\Framework\DataObject $request - * @return $this|bool|\Magento\Framework\DataObject + * @param DataObject $request + * @return $this|bool|DataObject * @deprecated 100.2.6 * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function proccessAdditionalValidation(\Magento\Framework\DataObject $request) + public function proccessAdditionalValidation(DataObject $request) { return $this->processAdditionalValidation($request); } @@ -345,9 +348,7 @@ public function proccessAdditionalValidation(\Magento\Framework\DataObject $requ */ public function isActive() { - $active = $this->getConfigData('active'); - - return $active == 1 || $active == 'true'; + return $this->carrierStatus->isEnabled($this->_code); } /** @@ -393,7 +394,7 @@ public function getSortOrder() /** * Check if the request has free shipping weight * - * @param \Magento\Quote\Model\Quote\Address\RateRequest $request + * @param RateRequest $request * @return bool */ private function hasFreeMethodWeight($request): bool @@ -410,7 +411,7 @@ private function hasFreeMethodWeight($request): bool /** * Allows free shipping when all product items have free shipping. * - * @param \Magento\Quote\Model\Quote\Address\RateRequest $request + * @param RateRequest $request * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) @@ -447,12 +448,12 @@ protected function _updateFreeMethodQuote($request) // phpstan:ignore $result = $this->_getQuotes(); if ($result && ($rates = $result->getAllRates()) && count($rates) > 0) { - if (count($rates) == 1 && $rates[0] instanceof \Magento\Quote\Model\Quote\Address\RateResult\Method) { + if (count($rates) == 1 && $rates[0] instanceof Method) { $price = $rates[0]->getPrice(); } if (count($rates) > 1) { foreach ($rates as $rate) { - if ($rate instanceof \Magento\Quote\Model\Quote\Address\RateResult\Method && + if ($rate instanceof Method && $rate->getMethod() == $freeMethod ) { $price = $rate->getPrice(); @@ -640,11 +641,11 @@ public function getCarrierCode() /** * Return content types of package * - * @param \Magento\Framework\DataObject $params + * @param DataObject $params * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function getContentTypes(\Magento\Framework\DataObject $params) + public function getContentTypes(DataObject $params) { return []; } @@ -670,10 +671,10 @@ public function getContentTypes(\Magento\Framework\DataObject $params) protected function filterDebugData($data) { try { - $xml = new \SimpleXMLElement($data); + $xml = new SimpleXMLElement($data); $this->filterXmlData($xml); $data = $xml->asXML(); - } catch (\Exception $e) { + } catch (Exception $e) { $this->_logger->critical($e); } return $data; @@ -682,16 +683,16 @@ protected function filterDebugData($data) /** * Recursive replace sensitive xml nodes values by specified mask. * - * @param \SimpleXMLElement $xml + * @param SimpleXMLElement $xml * @return void */ - private function filterXmlData(\SimpleXMLElement $xml) + private function filterXmlData(SimpleXMLElement $xml): void { - /** @var \SimpleXMLElement $child */ + /** @var SimpleXMLElement $child */ foreach ($xml->children() as $child) { if ($child->count()) { $this->filterXmlData($child); - } elseif (in_array((string) $child->getName(), $this->_debugReplacePrivateDataKeys)) { + } elseif (in_array($child->getName(), $this->_debugReplacePrivateDataKeys, true)) { $child[0] = self::DEBUG_KEYS_MASK; } } diff --git a/app/code/Magento/Shipping/Model/Carrier/AbstractCarrierOnline.php b/app/code/Magento/Shipping/Model/Carrier/AbstractCarrierOnline.php index 5e13688466618..24c8e39e15be1 100644 --- a/app/code/Magento/Shipping/Model/Carrier/AbstractCarrierOnline.php +++ b/app/code/Magento/Shipping/Model/Carrier/AbstractCarrierOnline.php @@ -4,13 +4,33 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Shipping\Model\Carrier; +use Magento\CatalogInventory\Api\StockRegistryInterface; +use Magento\Directory\Helper\Data; +use Magento\Directory\Model\CountryFactory; +use Magento\Directory\Model\CurrencyFactory; +use Magento\Directory\Model\RegionFactory; +use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\DataObject; use Magento\Framework\Exception\LocalizedException; use Magento\Quote\Model\Quote\Address\RateRequest; use Magento\Quote\Model\Quote\Address\RateResult\Error; +use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory; +use Magento\Quote\Model\Quote\Item; use Magento\Shipping\Model\Shipment\Request; use Magento\Framework\Xml\Security; +use Magento\Shipping\Model\Simplexml\ElementFactory; +use Magento\Shipping\Model\Tracking\Result; +use Magento\Shipping\Model\Tracking\Result\ErrorFactory; +use Magento\Shipping\Model\Tracking\Result\StatusFactory; +use Magento\Shipping\Model\Tracking\ResultFactory; +use Psr\Log\LoggerInterface; +use SimpleXMLElement; + +use function in_array; /** * Abstract online shipping carrier model @@ -22,6 +42,17 @@ */ abstract class AbstractCarrierOnline extends AbstractCarrier { + /** + * Samoa American + * Guam + * Northern Mariana Islands + * Palau + * Puerto Rico + * Virgin Islands US + * United States + */ + public const US_COUNTY_IDS = ['AS', 'GU', 'MP', 'PW', 'PR', 'VI', 'US']; + public const USA_COUNTRY_ID = 'US'; public const PUERTORICO_COUNTRY_ID = 'PR'; @@ -37,16 +68,19 @@ abstract class AbstractCarrierOnline extends AbstractCarrier /** * @var string + * + * @deprecated + * @see \Magento\Shipping\Model\Carrier\AbstractCarrier::isActive */ protected $_activeFlag = 'active'; /** - * @var \Magento\Directory\Helper\Data + * @var Data */ protected $_directoryData = null; /** - * @var \Magento\Shipping\Model\Simplexml\ElementFactory + * @var ElementFactory */ protected $_xmlElFactory; @@ -56,49 +90,49 @@ abstract class AbstractCarrierOnline extends AbstractCarrier protected $_rateFactory; /** - * @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory + * @var MethodFactory */ protected $_rateMethodFactory; /** - * @var \Magento\Shipping\Model\Tracking\ResultFactory + * @var ResultFactory */ protected $_trackFactory; /** - * @var \Magento\Shipping\Model\Tracking\Result\ErrorFactory + * @var ErrorFactory */ protected $_trackErrorFactory; /** - * @var \Magento\Shipping\Model\Tracking\Result\StatusFactory + * @var StatusFactory */ protected $_trackStatusFactory; /** - * @var \Magento\Directory\Model\RegionFactory + * @var RegionFactory */ protected $_regionFactory; /** - * @var \Magento\Directory\Model\CountryFactory + * @var CountryFactory */ protected $_countryFactory; /** - * @var \Magento\Directory\Model\CurrencyFactory + * @var CurrencyFactory */ protected $_currencyFactory; /** - * @var \Magento\CatalogInventory\Api\StockRegistryInterface + * @var StockRegistryInterface */ protected $stockRegistry; /** * Raw rate request data * - * @var \Magento\Framework\DataObject|null + * @var DataObject|null */ protected $_rawRequest = null; @@ -108,41 +142,24 @@ abstract class AbstractCarrierOnline extends AbstractCarrier protected $xmlSecurity; /** - * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig - * @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory - * @param \Psr\Log\LoggerInterface $logger - * @param Security $xmlSecurity - * @param \Magento\Shipping\Model\Simplexml\ElementFactory $xmlElFactory - * @param \Magento\Shipping\Model\Rate\ResultFactory $rateFactory - * @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory - * @param \Magento\Shipping\Model\Tracking\ResultFactory $trackFactory - * @param \Magento\Shipping\Model\Tracking\Result\ErrorFactory $trackErrorFactory - * @param \Magento\Shipping\Model\Tracking\Result\StatusFactory $trackStatusFactory - * @param \Magento\Directory\Model\RegionFactory $regionFactory - * @param \Magento\Directory\Model\CountryFactory $countryFactory - * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory - * @param \Magento\Directory\Helper\Data $directoryData - * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry - * @param array $data - * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( - \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, + ScopeConfigInterface $scopeConfig, \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory, - \Psr\Log\LoggerInterface $logger, + LoggerInterface $logger, Security $xmlSecurity, - \Magento\Shipping\Model\Simplexml\ElementFactory $xmlElFactory, + ElementFactory $xmlElFactory, \Magento\Shipping\Model\Rate\ResultFactory $rateFactory, - \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory, - \Magento\Shipping\Model\Tracking\ResultFactory $trackFactory, - \Magento\Shipping\Model\Tracking\Result\ErrorFactory $trackErrorFactory, - \Magento\Shipping\Model\Tracking\Result\StatusFactory $trackStatusFactory, - \Magento\Directory\Model\RegionFactory $regionFactory, - \Magento\Directory\Model\CountryFactory $countryFactory, - \Magento\Directory\Model\CurrencyFactory $currencyFactory, - \Magento\Directory\Helper\Data $directoryData, - \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, + MethodFactory $rateMethodFactory, + ResultFactory $trackFactory, + ErrorFactory $trackErrorFactory, + StatusFactory $trackStatusFactory, + RegionFactory $regionFactory, + CountryFactory $countryFactory, + CurrencyFactory $currencyFactory, + Data $directoryData, + StockRegistryInterface $stockRegistry, array $data = [] ) { $this->_xmlElFactory = $xmlElFactory; @@ -165,6 +182,9 @@ public function __construct( * * @param string $code * @return $this + * + * @deprecated + * @see \Magento\Shipping\Model\Carrier\AbstractCarrier::isActive */ public function setActiveFlag($code = 'active') { @@ -193,7 +213,7 @@ public function getTrackingInfo($tracking) { $result = $this->getTracking($tracking); - if ($result instanceof \Magento\Shipping\Model\Tracking\Result) { + if ($result instanceof Result) { $trackings = $result->getAllTrackings(); if ($trackings) { return $trackings[0]; @@ -266,7 +286,7 @@ public function getAllItems(RateRequest $request) $items = []; if ($request->getAllItems()) { foreach ($request->getAllItems() as $item) { - /* @var $item \Magento\Quote\Model\Quote\Item */ + /* @var $item Item */ if ($item->getProduct()->isVirtual() || $item->getParentItem()) { // Don't process children here - we will process (or already have processed) them below continue; @@ -291,13 +311,13 @@ public function getAllItems(RateRequest $request) /** * Processing additional validation to check if carrier applicable. * - * @param \Magento\Framework\DataObject $request - * @return $this|bool|\Magento\Framework\DataObject + * @param DataObject $request + * @return $this|bool|DataObject * @deprecated 100.2.6 * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ - public function proccessAdditionalValidation(\Magento\Framework\DataObject $request) + public function proccessAdditionalValidation(DataObject $request) { return $this->processAdditionalValidation($request); } @@ -305,13 +325,13 @@ public function proccessAdditionalValidation(\Magento\Framework\DataObject $requ /** * Processing additional validation to check if carrier applicable. * - * @param \Magento\Framework\DataObject $request - * @return $this|bool|\Magento\Framework\DataObject + * @param DataObject $request + * @return $this|bool|DataObject * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @since 100.2.6 */ - public function processAdditionalValidation(\Magento\Framework\DataObject $request) + public function processAdditionalValidation(DataObject $request) { //Skip by item validation if there is no items in request if (!count($this->getAllItems($request))) { @@ -324,7 +344,7 @@ public function processAdditionalValidation(\Magento\Framework\DataObject $reque $defaultErrorMsg = __('The shipping module is not available.'); $showMethod = $this->getConfigData('showmethod'); - /** @var $item \Magento\Quote\Model\Quote\Item */ + /** @var $item Item */ foreach ($this->getAllItems($request) as $item) { $product = $item->getProduct(); if ($product && $product->getId()) { @@ -364,7 +384,8 @@ public function processAdditionalValidation(\Magento\Framework\DataObject $reque $error->setErrorMessage($errorMsg); return $error; - } elseif ($errorMsg) { + } + if ($errorMsg) { return false; } @@ -438,10 +459,10 @@ protected function _prepareServiceName($name) /** * Prepare shipment request. Validate and correct request information * - * @param \Magento\Framework\DataObject $request + * @param DataObject $request * @return void */ - protected function _prepareShipmentRequest(\Magento\Framework\DataObject $request) + protected function _prepareShipmentRequest(DataObject $request) { $phonePattern = '/[\s\_\-\(\)]+/'; $phoneNumber = $request->getShipperContactPhoneNumber(); @@ -456,8 +477,8 @@ protected function _prepareShipmentRequest(\Magento\Framework\DataObject $reques * Do request to shipment * * @param Request $request - * @return \Magento\Framework\DataObject - * @throws \Magento\Framework\Exception\LocalizedException + * @return DataObject + * @throws LocalizedException */ public function requestToShipment($request) { @@ -473,26 +494,26 @@ public function requestToShipment($request) $request->setPackageId($packageId); $request->setPackagingType($package['params']['container']); $request->setPackageWeight($package['params']['weight']); - $request->setPackageParams(new \Magento\Framework\DataObject($package['params'])); + $request->setPackageParams(new DataObject($package['params'])); $request->setPackageItems($package['items']); $result = $this->_doShipmentRequest($request); if ($result->hasErrors()) { $this->rollBack($data); break; - } else { - $data[] = [ - 'tracking_number' => $result->getTrackingNumber(), - 'label_content' => $result->getShippingLabelContent(), - ]; } + + $data[] = [ + 'tracking_number' => $result->getTrackingNumber(), + 'label_content' => $result->getShippingLabelContent(), + ]; if (!isset($isFirstRequest)) { $request->setMasterTrackingId($result->getTrackingNumber()); $isFirstRequest = false; } } - $response = new \Magento\Framework\DataObject(['info' => $data]); + $response = new DataObject(['info' => $data]); if ($result->getErrors()) { $response->setErrors($result->getErrors()); } @@ -504,8 +525,8 @@ public function requestToShipment($request) * Do request to RMA shipment * * @param Request $request - * @return \Magento\Framework\DataObject - * @throws \Magento\Framework\Exception\LocalizedException + * @return DataObject + * @throws LocalizedException */ public function returnOfShipment($request) { @@ -522,7 +543,7 @@ public function returnOfShipment($request) $request->setPackageId($packageId); $request->setPackagingType($package['params']['container']); $request->setPackageWeight($package['params']['weight']); - $request->setPackageParams(new \Magento\Framework\DataObject($package['params'])); + $request->setPackageParams(new DataObject($package['params'])); $request->setPackageItems($package['items']); $result = $this->_doShipmentRequest($request); @@ -541,7 +562,7 @@ public function returnOfShipment($request) } } - $response = new \Magento\Framework\DataObject(['info' => $data]); + $response = new DataObject(['info' => $data]); if ($result->getErrors()) { $response->setErrors($result->getErrors()); } @@ -566,10 +587,10 @@ public function rollBack($data) /** * Do shipment request to carrier web service, obtain Print Shipping Labels and process errors in response * - * @param \Magento\Framework\DataObject $request - * @return \Magento\Framework\DataObject + * @param DataObject $request + * @return DataObject */ - abstract protected function _doShipmentRequest(\Magento\Framework\DataObject $request); + abstract protected function _doShipmentRequest(DataObject $request); /** * Check is Country U.S. Possessions and Trust Territories @@ -579,25 +600,7 @@ abstract protected function _doShipmentRequest(\Magento\Framework\DataObject $re */ protected function _isUSCountry($countyId) { - switch ($countyId) { - case 'AS': - // Samoa American - case 'GU': - // Guam - case 'MP': - // Northern Mariana Islands - case 'PW': - // Palau - case 'PR': - // Puerto Rico - case 'VI': - // Virgin Islands US - case 'US': - // United States - return true; - } - - return false; + return in_array($countyId, self::US_COUNTY_IDS, true); } /** @@ -616,7 +619,7 @@ public function isGirthAllowed($countyDest = null, $carrierMethodCode = null) /** * Set Raw Request * - * @param \Magento\Framework\DataObject|null $request + * @param DataObject|null $request * @return $this */ public function setRawRequest($request) @@ -635,7 +638,7 @@ public function setRawRequest($request) */ public function getMethodPrice($cost, $method = '') { - return $method == $this->getConfigData( + return $method === $this->getConfigData( $this->_freeMethod ) && $this->getConfigFlag( 'free_shipping_enable' @@ -651,7 +654,7 @@ public function getMethodPrice($cost, $method = '') * * @param string $xmlContent * @param string $customSimplexml - * @return \SimpleXMLElement|bool + * @return SimpleXMLElement|bool * @throws LocalizedException */ public function parseXml($xmlContent, $customSimplexml = 'SimpleXMLElement') @@ -660,9 +663,7 @@ public function parseXml($xmlContent, $customSimplexml = 'SimpleXMLElement') throw new LocalizedException(__('The security validation of the XML document has failed.')); } - $xmlElement = simplexml_load_string($xmlContent, $customSimplexml); - - return $xmlElement; + return simplexml_load_string($xmlContent, $customSimplexml); } /** @@ -704,8 +705,8 @@ protected function getErrorMessage() $error->setCarrierTitle($this->getConfigData('title')); $error->setErrorMessage($this->getConfigData('specificerrmsg')); return $error; - } else { - return false; } + + return false; } } diff --git a/app/code/Magento/Shipping/Model/Config.php b/app/code/Magento/Shipping/Model/Config.php index 565901ebe8ef0..afe44f1082a28 100644 --- a/app/code/Magento/Shipping/Model/Config.php +++ b/app/code/Magento/Shipping/Model/Config.php @@ -9,9 +9,11 @@ namespace Magento\Shipping\Model; use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\App\ObjectManager; use Magento\Framework\DataObject; use Magento\Shipping\Model\Carrier\AbstractCarrierInterface; -use Magento\Store\Model\ScopeInterface; +use Magento\Shipping\Model\Config\Carriers as CarriersConfig; +use Magento\Shipping\Model\Config\CarrierStatus; /** * Config model for shipping @@ -23,13 +25,13 @@ class Config extends DataObject /** * Shipping origin settings */ - const XML_PATH_ORIGIN_COUNTRY_ID = 'shipping/origin/country_id'; + public const XML_PATH_ORIGIN_COUNTRY_ID = 'shipping/origin/country_id'; - const XML_PATH_ORIGIN_REGION_ID = 'shipping/origin/region_id'; + public const XML_PATH_ORIGIN_REGION_ID = 'shipping/origin/region_id'; - const XML_PATH_ORIGIN_CITY = 'shipping/origin/city'; + public const XML_PATH_ORIGIN_CITY = 'shipping/origin/city'; - const XML_PATH_ORIGIN_POSTCODE = 'shipping/origin/postcode'; + public const XML_PATH_ORIGIN_POSTCODE = 'shipping/origin/postcode'; /** * Core store config @@ -44,19 +46,26 @@ class Config extends DataObject protected $_carrierFactory; /** - * Constructor - * - * @param ScopeConfigInterface $scopeConfig - * @param CarrierFactory $carrierFactory - * @param array $data + * @var CarriersConfig + */ + private $carriersConfig; + + /** + * @var CarrierStatus */ + private $carrierStatus; + public function __construct( ScopeConfigInterface $scopeConfig, CarrierFactory $carrierFactory, - array $data = [] + array $data = [], + CarriersConfig $carriersConfig = null, + CarrierStatus $carrierStatus = null ) { $this->_scopeConfig = $scopeConfig; $this->_carrierFactory = $carrierFactory; + $this->carriersConfig = $carriersConfig ?? ObjectManager::getInstance()->get(CarriersConfig::class); + $this->carrierStatus = $carrierStatus ?? ObjectManager::getInstance()->get(CarrierStatus::class); parent::__construct($data); } @@ -69,13 +78,9 @@ public function __construct( public function getActiveCarriers($store = null) { $carriers = []; - $config = $this->getCarriersConfig($store); + $config = $this->carriersConfig->getConfig($store); foreach (array_keys($config) as $carrierCode) { - if ($this->_scopeConfig->isSetFlag( - 'carriers/' . $carrierCode . '/active', - ScopeInterface::SCOPE_STORE, - $store - )) { + if ($this->carrierStatus->isEnabled($carrierCode, $store)) { $carrierModel = $this->_carrierFactory->create($carrierCode, $store); if ($carrierModel) { $carriers[$carrierCode] = $carrierModel; @@ -95,7 +100,7 @@ public function getActiveCarriers($store = null) public function getAllCarriers($store = null) { $carriers = []; - $config = $this->getCarriersConfig($store); + $config = $this->carriersConfig->getConfig($store); foreach (array_keys($config) as $carrierCode) { $model = $this->_carrierFactory->create($carrierCode, $store); if ($model) { @@ -105,15 +110,4 @@ public function getAllCarriers($store = null) return $carriers; } - - /** - * Returns carriers config by store - * - * @param mixed $store - * @return array - */ - private function getCarriersConfig($store = null): array - { - return $this->_scopeConfig->getValue('carriers', ScopeInterface::SCOPE_STORE, $store) ?: []; - } } diff --git a/app/code/Magento/Shipping/Model/Config/CarrierStatus.php b/app/code/Magento/Shipping/Model/Config/CarrierStatus.php new file mode 100644 index 0000000000000..52ecc5c6a150c --- /dev/null +++ b/app/code/Magento/Shipping/Model/Config/CarrierStatus.php @@ -0,0 +1,41 @@ +scopeConfig = $scopeConfig; + $this->configField = $configField; + } + + /** + * @param null|string|int $store + */ + public function isEnabled(string $carrierCode, $store = null): bool + { + return $this->scopeConfig->isSetFlag( + 'carriers/' . $carrierCode . '/' . $this->configField, + ScopeInterface::SCOPE_STORE, + $store + ); + } +} diff --git a/app/code/Magento/Shipping/Model/Config/Carriers.php b/app/code/Magento/Shipping/Model/Config/Carriers.php new file mode 100644 index 0000000000000..f683097a8f52a --- /dev/null +++ b/app/code/Magento/Shipping/Model/Config/Carriers.php @@ -0,0 +1,36 @@ +scopeConfig = $scopeConfig; + } + + /** + * Returns carriers config by store + * + * @param null|string|int $storeId + */ + public function getConfig($storeId = null): array + { + return $this->scopeConfig->getValue('carriers', ScopeInterface::SCOPE_STORE, $storeId) ?: []; + } +} diff --git a/app/code/Magento/Shipping/Model/Shipping.php b/app/code/Magento/Shipping/Model/Shipping.php index 7e5a9574e8b92..c81933c428efa 100644 --- a/app/code/Magento/Shipping/Model/Shipping.php +++ b/app/code/Magento/Shipping/Model/Shipping.php @@ -4,20 +4,37 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Shipping\Model; +use Magento\Catalog\Model\Product\Type; +use Magento\CatalogInventory\Api\StockRegistryInterface; +use Magento\Directory\Model\RegionFactory; +use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\ObjectManager; +use Magento\Framework\DataObject; +use Magento\Framework\Math\Division; use Magento\Quote\Model\Quote\Address\RateCollectorInterface; use Magento\Quote\Model\Quote\Address\RateRequest; use Magento\Quote\Model\Quote\Address\RateRequestFactory; use Magento\Quote\Model\Quote\Address\RateResult\Error; +use Magento\Quote\Model\Quote\Item; use Magento\Sales\Model\Order\Shipment; use Magento\Shipping\Model\Carrier\AbstractCarrier; +use Magento\Shipping\Model\Config\Carriers as CarriersConfig; +use Magento\Shipping\Model\Config\CarrierStatus; use Magento\Shipping\Model\Rate\CarrierResult; use Magento\Shipping\Model\Rate\CarrierResultFactory; use Magento\Shipping\Model\Rate\PackageResult; use Magento\Shipping\Model\Rate\PackageResultFactory; use Magento\Shipping\Model\Rate\Result; +use Magento\Shipping\Model\Rate\ResultFactory; +use Magento\Shipping\Model\Shipment\RequestFactory; +use Magento\Store\Api\Data\StoreInterface; +use Magento\Store\Model\ScopeInterface; +use Magento\Store\Model\StoreManagerInterface; +use RuntimeException; /** * @inheritDoc @@ -36,7 +53,7 @@ class Shipping implements RateCollectorInterface /** * Cached result * - * @var \Magento\Shipping\Model\Rate\Result + * @var Result */ protected $_result = null; @@ -44,28 +61,31 @@ class Shipping implements RateCollectorInterface * Part of carrier xml config path * * @var string + * + * @deprecated The config field must be 'active' + * @see \Magento\Shipping\Model\Carrier\AbstractCarrier::isActive */ protected $_availabilityConfigField = 'active'; /** * Core store config * - * @var \Magento\Framework\App\Config\ScopeConfigInterface + * @var ScopeConfigInterface */ protected $_scopeConfig; /** - * @var \Magento\Store\Model\StoreManagerInterface + * @var StoreManagerInterface */ protected $_storeManager; /** - * @var \Magento\Shipping\Model\Config + * @var Config */ protected $_shippingConfig; /** - * @var \Magento\Shipping\Model\CarrierFactory + * @var CarrierFactory */ protected $_carrierFactory; @@ -80,86 +100,72 @@ class Shipping implements RateCollectorInterface protected $_shipmentRequestFactory; /** - * @var \Magento\Directory\Model\RegionFactory + * @var RegionFactory */ protected $_regionFactory; /** - * @var \Magento\Framework\Math\Division + * @var Division */ protected $mathDivision; /** - * @var \Magento\CatalogInventory\Api\StockRegistryInterface + * @var StockRegistryInterface */ protected $stockRegistry; - /** - * @var RateRequestFactory - */ - private $rateRequestFactory; + private RateRequestFactory $rateRequestFactory; - /** - * @var PackageResultFactory - */ - private $packageResultFactory; + private PackageResultFactory $packageResultFactory; + + private CarriersConfig $carriersConfig; + + private CarrierStatus $carrierStatus; /** - * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig - * @param \Magento\Shipping\Model\Config $shippingConfig - * @param \Magento\Store\Model\StoreManagerInterface $storeManager - * @param \Magento\Shipping\Model\CarrierFactory $carrierFactory - * @param \Magento\Shipping\Model\Rate\CarrierResultFactory $rateResultFactory - * @param \Magento\Shipping\Model\Shipment\RequestFactory $shipmentRequestFactory - * @param \Magento\Directory\Model\RegionFactory $regionFactory - * @param \Magento\Framework\Math\Division $mathDivision - * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry - * @param RateRequestFactory $rateRequestFactory - * @param PackageResultFactory|null $packageResultFactory - * @param CarrierResultFactory|null $carrierResultFactory - * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function __construct( - \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, - \Magento\Shipping\Model\Config $shippingConfig, - \Magento\Store\Model\StoreManagerInterface $storeManager, - \Magento\Shipping\Model\CarrierFactory $carrierFactory, - \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory, - \Magento\Shipping\Model\Shipment\RequestFactory $shipmentRequestFactory, - \Magento\Directory\Model\RegionFactory $regionFactory, - \Magento\Framework\Math\Division $mathDivision, - \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, + ScopeConfigInterface $scopeConfig, + Config $shippingConfig, + StoreManagerInterface $storeManager, + CarrierFactory $carrierFactory, + ResultFactory $rateResultFactory, + RequestFactory $shipmentRequestFactory, + RegionFactory $regionFactory, + Division $mathDivision, + StockRegistryInterface $stockRegistry, RateRequestFactory $rateRequestFactory = null, ?PackageResultFactory $packageResultFactory = null, - ?CarrierResultFactory $carrierResultFactory = null + ?CarrierResultFactory $carrierResultFactory = null, + CarriersConfig $carriersConfig = null, + CarrierStatus $carrierStatus = null ) { $this->_scopeConfig = $scopeConfig; $this->_shippingConfig = $shippingConfig; $this->_storeManager = $storeManager; $this->_carrierFactory = $carrierFactory; - $rateResultFactory = $carrierResultFactory ?? ObjectManager::getInstance()->get(CarrierResultFactory::class); - $this->_rateResultFactory = $rateResultFactory; + $this->_rateResultFactory = $carrierResultFactory + ?? ObjectManager::getInstance()->get(CarrierResultFactory::class); $this->_shipmentRequestFactory = $shipmentRequestFactory; $this->_regionFactory = $regionFactory; $this->mathDivision = $mathDivision; $this->stockRegistry = $stockRegistry; - $this->rateRequestFactory = $rateRequestFactory ?: ObjectManager::getInstance()->get(RateRequestFactory::class); + $this->rateRequestFactory = $rateRequestFactory ?? ObjectManager::getInstance()->get(RateRequestFactory::class); $this->packageResultFactory = $packageResultFactory ?? ObjectManager::getInstance()->get(PackageResultFactory::class); + $this->carriersConfig = $carriersConfig ?? ObjectManager::getInstance()->get(CarriersConfig::class); + $this->carrierStatus = $carrierStatus ?? ObjectManager::getInstance()->create(CarrierStatus::class, ['configField' => $this->_availabilityConfigField]); } /** * Get shipping rate result model * - * @return \Magento\Shipping\Model\Rate\Result|CarrierResult + * @return Result|CarrierResult */ public function getResult() { - if (empty($this->_result)) { - $this->_result = $this->_rateResultFactory->create(); - } - return $this->_result; + return $this->_result ?? $this->_result = $this->_rateResultFactory->create(); } /** @@ -187,7 +193,7 @@ public function resetResult() /** * Retrieve configuration model * - * @return \Magento\Shipping\Model\Config + * @return Config */ public function getConfig() { @@ -197,36 +203,36 @@ public function getConfig() /** * Retrieve all methods for supplied shipping data * - * @param \Magento\Quote\Model\Quote\Address\RateRequest $request + * @param RateRequest $request * @return $this * @todo make it ordered */ - public function collectRates(\Magento\Quote\Model\Quote\Address\RateRequest $request) + public function collectRates(RateRequest $request) { $storeId = $request->getStoreId(); if (!$request->getOrig()) { $request->setCountryId( $this->_scopeConfig->getValue( Shipment::XML_PATH_STORE_COUNTRY_ID, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + ScopeInterface::SCOPE_STORE, $request->getStore() ) )->setRegionId( $this->_scopeConfig->getValue( Shipment::XML_PATH_STORE_REGION_ID, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + ScopeInterface::SCOPE_STORE, $request->getStore() ) )->setCity( $this->_scopeConfig->getValue( Shipment::XML_PATH_STORE_CITY, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + ScopeInterface::SCOPE_STORE, $request->getStore() ) )->setPostcode( $this->_scopeConfig->getValue( Shipment::XML_PATH_STORE_ZIP, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + ScopeInterface::SCOPE_STORE, $request->getStore() ) ); @@ -234,11 +240,7 @@ public function collectRates(\Magento\Quote\Model\Quote\Address\RateRequest $req $limitCarrier = $request->getLimitCarrier(); if (!$limitCarrier) { - $carriers = $this->_scopeConfig->getValue( - 'carriers', - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, - $storeId - ); + $carriers = $this->carriersConfig->getConfig($storeId); foreach ($carriers as $carrierCode => $carrierConfig) { $this->collectCarrierRates($carrierCode, $request); @@ -250,7 +252,7 @@ public function collectRates(\Magento\Quote\Model\Quote\Address\RateRequest $req foreach ($limitCarrier as $carrierCode) { $carrierConfig = $this->_scopeConfig->getValue( 'carriers/' . $carrierCode, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + ScopeInterface::SCOPE_STORE, $storeId ); if (!$carrierConfig) { @@ -266,22 +268,19 @@ public function collectRates(\Magento\Quote\Model\Quote\Address\RateRequest $req /** * Prepare carrier to find rates. * - * @param string $carrierCode - * @param RateRequest $request - * @return AbstractCarrier - * @throws \RuntimeException + * @throws RuntimeException */ private function prepareCarrier(string $carrierCode, RateRequest $request): AbstractCarrier { - $carrier = $this->isShippingCarrierAvailable($carrierCode, $request->getStoreId()) + $carrier = $this->carrierStatus->isEnabled($carrierCode, $request->getStoreId()) ? $this->_carrierFactory->create($carrierCode, $request->getStoreId()) : null; if (!$carrier) { - throw new \RuntimeException('Failed to initialize carrier'); + throw new RuntimeException('Failed to initialize carrier'); } $carrier->setActiveFlag($this->_availabilityConfigField); $result = $carrier->checkAvailableShipCountries($request); - if (false !== $result && !$result instanceof Error) { + if ($result !== false && !$result instanceof Error) { $result = $carrier->processAdditionalValidation($request); } if (!$result) { @@ -289,10 +288,12 @@ private function prepareCarrier(string $carrierCode, RateRequest $request): Abst * Result will be false if the admin set not to show the shipping module * if the delivery country is not within specific countries */ - throw new \RuntimeException('Cannot collect rates for given request'); - } elseif ($result instanceof Error) { + throw new RuntimeException('Cannot collect rates for given request'); + } + + if ($result instanceof Error) { $this->getResult()->append($result); - throw new \RuntimeException('Error occurred while preparing a carrier'); + throw new RuntimeException('Error occurred while preparing a carrier'); } return $carrier; @@ -309,11 +310,11 @@ public function collectCarrierRates($carrierCode, $request) { try { $carrier = $this->prepareCarrier($carrierCode, $request); - } catch (\RuntimeException $exception) { + } catch (RuntimeException $exception) { return $this; } - /** @var Result|\Magento\Quote\Model\Quote\Address\RateResult\Error|null $result */ + /** @var Result|Error|null $result */ $result = null; if ($carrier->getConfigData('shipment_requesttype')) { $packages = $this->composePackagesForCarrier($carrier, $request); @@ -325,9 +326,8 @@ public function collectCarrierRates($carrierCode, $request) $packageResult = $carrier->collectRates($request); if (!$packageResult) { return $this; - } else { - $result->appendPackageResult($packageResult, 1); } + $result->appendPackageResult($packageResult, 1); } } if (!$result) { @@ -337,7 +337,9 @@ public function collectCarrierRates($carrierCode, $request) if (!$result) { return $this; - } elseif ($result instanceof Result) { + } + + if ($result instanceof Result) { $this->getResult()->appendResult($result, $carrier->getConfigData('showmethod') != 0); } else { $this->getResult()->append($result); @@ -351,8 +353,8 @@ public function collectCarrierRates($carrierCode, $request) * * Divides order into items and items into parts if it's necessary * - * @param \Magento\Shipping\Model\Carrier\AbstractCarrier $carrier - * @param \Magento\Quote\Model\Quote\Address\RateRequest $request + * @param AbstractCarrier $carrier + * @param RateRequest $request * @return array [int, float] * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) @@ -365,11 +367,9 @@ public function composePackagesForCarrier($carrier, $request) $maxWeight = (double)$carrier->getConfigData('max_package_weight'); - /** @var $item \Magento\Quote\Model\Quote\Item */ + /** @var $item Item */ foreach ($allItems as $item) { - if ($item->getProductType() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE - && $item->getProduct()->getShipmentType() - ) { + if ($item->getProductType() == Type::TYPE_BUNDLE && $item->getProduct()->getShipmentType()) { continue; } @@ -392,9 +392,7 @@ public function composePackagesForCarrier($carrier, $request) } $itemWeight = (float) $item->getWeight(); - if ($item->getIsQtyDecimal() - && $item->getProductType() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE - ) { + if ($item->getIsQtyDecimal() && $item->getProductType() != Type::TYPE_BUNDLE) { $productId = $item->getProduct()->getId(); $itemWeightWhole = $itemWeight * $item->getQty(); $stockItem = $this->stockRegistry->getStockItem($productId, $item->getStore()->getWebsiteId()); @@ -424,7 +422,7 @@ public function composePackagesForCarrier($carrier, $request) if ($changeQty && !$item->getParentItem() && $item->getIsQtyDecimal() - && $item->getProductType() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE + && $item->getProductType() != Type::TYPE_BUNDLE ) { $qty = 1; } @@ -443,7 +441,7 @@ public function composePackagesForCarrier($carrier, $request) } else { $weightItems[] = array_fill( 0, - $qty, + (int)ceil($qty), [ 'weight' => $itemWeight, 'price' => $item->getBasePrice() @@ -494,13 +492,13 @@ protected function _makePieces(array $orderItems, float $maxPackageWeight): arra /** * Collect rates by address * - * @param \Magento\Framework\DataObject $address + * @param DataObject $address * @param null|bool|array $limitCarrier * @return $this */ - public function collectRatesByAddress(\Magento\Framework\DataObject $address, $limitCarrier = null) + public function collectRatesByAddress(DataObject $address, $limitCarrier = null) { - /** @var $request \Magento\Quote\Model\Quote\Address\RateRequest */ + /** @var $request RateRequest */ $request = $this->rateRequestFactory->create(); $request->setAllItems($address->getAllItems()); $request->setDestCountryId($address->getCountryId()); @@ -512,7 +510,7 @@ public function collectRatesByAddress(\Magento\Framework\DataObject $address, $l $request->setFreeMethodWeight($address->getFreeMethodWeight()); $request->setPackageQty($address->getItemQty()); - /** @var \Magento\Store\Api\Data\StoreInterface $store */ + /** @var StoreInterface $store */ $store = $this->_storeManager->getStore(); $request->setStoreId($store->getId()); $request->setWebsiteId($store->getWebsiteId()); @@ -530,26 +528,13 @@ public function collectRatesByAddress(\Magento\Framework\DataObject $address, $l * * @param string $code * @return $this + * + * @deprecated + * \Magento\Shipping\Model\Carrier\AbstractCarrier::isActive */ public function setCarrierAvailabilityConfigField($code = 'active') { $this->_availabilityConfigField = $code; return $this; } - - /** - * Checks availability of carrier. - * - * @param string $carrierCode - * @param null|int $storeId - * @return bool - */ - private function isShippingCarrierAvailable(string $carrierCode, ?int $storeId = null): bool - { - return $this->_scopeConfig->isSetFlag( - 'carriers/' . $carrierCode . '/' . $this->_availabilityConfigField, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, - $storeId - ); - } } diff --git a/app/code/Magento/Shipping/Test/Unit/Model/ConfigTest.php b/app/code/Magento/Shipping/Test/Unit/Model/ConfigTest.php index e676b698c7f0e..09a899c6aa28c 100644 --- a/app/code/Magento/Shipping/Test/Unit/Model/ConfigTest.php +++ b/app/code/Magento/Shipping/Test/Unit/Model/ConfigTest.php @@ -60,7 +60,13 @@ protected function setUp(): void $this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class); $this->carrierFactoryMock = $this->createMock(CarrierFactory::class); - $this->model = new Config($this->scopeConfigMock, $this->carrierFactoryMock, []); + $this->model = new Config( + $this->scopeConfigMock, + $this->carrierFactoryMock, + [], + new Config\Carriers($this->scopeConfigMock), + new Config\CarrierStatus($this->scopeConfigMock) + ); } /** diff --git a/app/code/Magento/Shipping/Test/Unit/Model/ShippingTest.php b/app/code/Magento/Shipping/Test/Unit/Model/ShippingTest.php index a41457710c774..f07758e7cbaaf 100644 --- a/app/code/Magento/Shipping/Test/Unit/Model/ShippingTest.php +++ b/app/code/Magento/Shipping/Test/Unit/Model/ShippingTest.php @@ -17,6 +17,7 @@ use Magento\Quote\Model\Quote\Item as QuoteItem; use Magento\Shipping\Model\Carrier\AbstractCarrierInterface; use Magento\Shipping\Model\CarrierFactory; +use Magento\Shipping\Model\Config\CarrierStatus; use Magento\Shipping\Model\Shipping; use Magento\Store\Model\Store; use PHPUnit\Framework\MockObject\MockObject; @@ -76,6 +77,7 @@ protected function setUp(): void 'stockRegistry' => $this->stockRegistry, 'carrierFactory' => $this->getCarrierFactory(), 'scopeConfig' => $this->scopeConfig, + 'carrierStatus' => new CarrierStatus($this->scopeConfig) ] ); }