From cfd5c22848f82f29f69cd42ffbcc8e09fa848568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Szubert?= Date: Thu, 19 Dec 2019 05:00:50 +0100 Subject: [PATCH] Fix #25390 - fix backward incompatible constructor in UPS carrier, cleanup of class imports --- app/code/Magento/Ups/Model/Carrier.php | 160 ++++++++++++++----------- 1 file changed, 90 insertions(+), 70 deletions(-) diff --git a/app/code/Magento/Ups/Model/Carrier.php b/app/code/Magento/Ups/Model/Carrier.php index 9e33b86ea8215..103ba9d3fb4b7 100644 --- a/app/code/Magento/Ups/Model/Carrier.php +++ b/app/code/Magento/Ups/Model/Carrier.php @@ -7,6 +7,12 @@ namespace Magento\Ups\Model; +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\App\ObjectManager; use Magento\Framework\Async\CallbackDeferred; use Magento\Framework\DataObject; @@ -15,16 +21,30 @@ use Magento\Framework\HTTP\AsyncClient\Request; use Magento\Framework\HTTP\AsyncClientInterface; use Magento\Framework\HTTP\ClientFactory; +use Magento\Framework\Locale\FormatInterface; use Magento\Framework\Xml\Security; use Magento\Quote\Model\Quote\Address\RateRequest; use Magento\Quote\Model\Quote\Address\RateResult\Error; +use Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory as RateErrorFactory; +use Magento\Quote\Model\Quote\Address\RateResult\MethodFactory as RateMethodFactory; +use Magento\Sales\Model\Order\Shipment as OrderShipment; use Magento\Shipping\Model\Carrier\AbstractCarrierOnline; use Magento\Shipping\Model\Carrier\CarrierInterface; use Magento\Shipping\Model\Rate\Result; use Magento\Shipping\Model\Rate\Result\ProxyDeferredFactory; +use Magento\Shipping\Model\Rate\ResultFactory as RateFactory; use Magento\Shipping\Model\Simplexml\Element; +use Magento\Shipping\Model\Simplexml\ElementFactory; +use Magento\Shipping\Model\Tracking\Result\ErrorFactory as TrackErrorFactory; +use Magento\Shipping\Model\Tracking\Result\StatusFactory as TrackStatusFactory; +use Magento\Shipping\Model\Tracking\ResultFactory as TrackFactory; +use Magento\Store\Model\ScopeInterface; use Magento\Ups\Helper\Config; use Magento\Shipping\Model\Shipment\Request as Shipment; +use Psr\Log\LoggerInterface; +use RuntimeException; +use Throwable; +use Zend_Http_Client; /** * UPS shipping implementation @@ -117,12 +137,12 @@ class Carrier extends AbstractCarrierOnline implements CarrierInterface protected $_customizableContainerTypes = ['CP', 'CSP']; /** - * @var \Magento\Framework\Locale\FormatInterface + * @var FormatInterface */ protected $_localeFormat; /** - * @var \Psr\Log\LoggerInterface + * @var LoggerInterface */ protected $_logger; @@ -149,22 +169,22 @@ class Carrier extends AbstractCarrierOnline implements CarrierInterface private $deferredProxyFactory; /** - * @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 RateErrorFactory $rateErrorFactory + * @param 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 \Magento\Framework\Locale\FormatInterface $localeFormat + * @param ElementFactory $xmlElFactory + * @param RateFactory $rateFactory + * @param RateMethodFactory $rateMethodFactory + * @param TrackFactory $trackFactory + * @param TrackErrorFactory $trackErrorFactory + * @param TrackStatusFactory $trackStatusFactory + * @param RegionFactory $regionFactory + * @param CountryFactory $countryFactory + * @param CurrencyFactory $currencyFactory + * @param Data $directoryData + * @param StockRegistryInterface $stockRegistry + * @param FormatInterface $localeFormat * @param Config $configHelper * @param ClientFactory $httpClientFactory * @param array $data @@ -175,27 +195,27 @@ class Carrier extends AbstractCarrierOnline implements CarrierInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function __construct( - \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, - \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory, - \Psr\Log\LoggerInterface $logger, + ScopeConfigInterface $scopeConfig, + RateErrorFactory $rateErrorFactory, + LoggerInterface $logger, Security $xmlSecurity, - \Magento\Shipping\Model\Simplexml\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, - \Magento\Framework\Locale\FormatInterface $localeFormat, + ElementFactory $xmlElFactory, + RateFactory $rateFactory, + RateMethodFactory $rateMethodFactory, + TrackFactory $trackFactory, + TrackErrorFactory $trackErrorFactory, + TrackStatusFactory $trackStatusFactory, + RegionFactory $regionFactory, + CountryFactory $countryFactory, + CurrencyFactory $currencyFactory, + Data $directoryData, + StockRegistryInterface $stockRegistry, + FormatInterface $localeFormat, Config $configHelper, ClientFactory $httpClientFactory, array $data = [], ?AsyncClientInterface $asyncHttpClient = null, - ?ProxyDeferredFactory $proxyDeferredFactory + ?ProxyDeferredFactory $proxyDeferredFactory = null ) { parent::__construct( $scopeConfig, @@ -265,7 +285,7 @@ public function setRequest(RateRequest $request) { $this->_request = $request; - $rowRequest = new \Magento\Framework\DataObject(); + $rowRequest = new DataObject(); if ($request->getLimitMethod()) { $rowRequest->setAction($this->configHelper->getCode('action', 'single')); @@ -300,8 +320,8 @@ public function setRequest(RateRequest $request) $origCountry = $request->getOrigCountry(); } else { $origCountry = $this->_scopeConfig->getValue( - \Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_COUNTRY_ID, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + OrderShipment::XML_PATH_STORE_COUNTRY_ID, + ScopeInterface::SCOPE_STORE, $request->getStoreId() ); } @@ -312,8 +332,8 @@ public function setRequest(RateRequest $request) $origRegionCode = $request->getOrigRegionCode(); } else { $origRegionCode = $this->_scopeConfig->getValue( - \Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_REGION_ID, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + OrderShipment::XML_PATH_STORE_REGION_ID, + ScopeInterface::SCOPE_STORE, $request->getStoreId() ); } @@ -327,8 +347,8 @@ public function setRequest(RateRequest $request) } else { $rowRequest->setOrigPostal( $this->_scopeConfig->getValue( - \Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_ZIP, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + OrderShipment::XML_PATH_STORE_ZIP, + ScopeInterface::SCOPE_STORE, $request->getStoreId() ) ); @@ -339,8 +359,8 @@ public function setRequest(RateRequest $request) } else { $rowRequest->setOrigCity( $this->_scopeConfig->getValue( - \Magento\Sales\Model\Order\Shipment::XML_PATH_STORE_CITY, - \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + OrderShipment::XML_PATH_STORE_CITY, + ScopeInterface::SCOPE_STORE, $request->getStoreId() ) ); @@ -516,7 +536,7 @@ protected function _getCgiQuotes() if (!$url) { $url = $this->_defaultCgiGatewayUrl; } - $client = new \Zend_Http_Client(); + $client = new Zend_Http_Client(); $client->setUri($url); $client->setConfig(['maxredirects' => 0, 'timeout' => 30]); $client->setParameterGet($params); @@ -525,7 +545,7 @@ protected function _getCgiQuotes() $debugData['result'] = $responseBody; $this->_setCachedQuotes($params, $responseBody); - } catch (\Throwable $e) { + } catch (Throwable $e) { $debugData['result'] = ['error' => $e->getMessage(), 'code' => $e->getCode()]; $responseBody = ''; } @@ -727,7 +747,7 @@ protected function _getXmlQuotes() {$shipperStateProvince} - +
{$params['19_destPostal']} @@ -743,7 +763,7 @@ protected function _getXmlQuotes() $xmlParams .= << - +
{$params['15_origPostal']} @@ -1056,7 +1076,7 @@ protected function setXMLAccessRequest() * Get cgi tracking * * @param string[] $trackings - * @return \Magento\Shipping\Model\Tracking\ResultFactory + * @return TrackFactory */ protected function _getCgiTracking($trackings) { @@ -1321,13 +1341,13 @@ public function getAllowedMethods() /** * Form XML for shipment request * - * @param \Magento\Framework\DataObject $request + * @param DataObject $request * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ - protected function _formShipmentRequest(\Magento\Framework\DataObject $request) + protected function _formShipmentRequest(DataObject $request) { $packageParams = $request->getPackageParams(); $height = $packageParams->getHeight(); @@ -1339,7 +1359,7 @@ protected function _formShipmentRequest(\Magento\Framework\DataObject $request) $itemsDesc = []; $itemsShipment = $request->getPackageItems(); foreach ($itemsShipment as $itemShipment) { - $item = new \Magento\Framework\DataObject(); + $item = new DataObject(); $item->setData($itemShipment); $itemsDesc[] = $item->getName(); } @@ -1533,7 +1553,7 @@ protected function _formShipmentRequest(\Magento\Framework\DataObject $request) * Send and process shipment accept request * * @param Element $shipmentConfirmResponse - * @return \Magento\Framework\DataObject + * @return DataObject * @deprecated New asynchronous methods introduced. * @see requestToShipment */ @@ -1559,18 +1579,18 @@ protected function _sendShipmentAcceptRequest(Element $shipmentConfirmResponse) $xmlResponse = $deferredResponse->get()->getBody(); $debugData['result'] = $xmlResponse; $this->_setCachedQuotes($xmlRequest, $xmlResponse); - } catch (\Throwable $e) { + } catch (Throwable $e) { $debugData['result'] = ['error' => $e->getMessage(), 'code' => $e->getCode()]; $xmlResponse = ''; } try { $response = $this->_xmlElFactory->create(['data' => $xmlResponse]); - } catch (\Throwable $e) { + } catch (Throwable $e) { $debugData['result'] = ['error' => $e->getMessage(), 'code' => $e->getCode()]; } - $result = new \Magento\Framework\DataObject(); + $result = new DataObject(); if (isset($response->Error)) { $result->setErrors((string)$response->Error->ErrorDescription); } else { @@ -1609,7 +1629,7 @@ public function getShipAcceptUrl() * @param DataObject[] $packages * @return string[] Quote IDs. * @throws LocalizedException - * @throws \RuntimeException + * @throws RuntimeException */ private function requestQuotes(array $packages): array { @@ -1640,13 +1660,13 @@ private function requestQuotes(array $packages): array try { /** @var Element $response */ $response = $this->_xmlElFactory->create(['data' => $httpResponse->getBody()]); - } catch (\Throwable $e) { - throw new \RuntimeException($e->getMessage()); + } catch (Throwable $e) { + throw new RuntimeException($e->getMessage()); } if (isset($response->Response->Error) && in_array($response->Response->Error->ErrorSeverity, ['Hard', 'Transient']) ) { - throw new \RuntimeException((string)$response->Response->Error->ErrorDescription); + throw new RuntimeException((string)$response->Response->Error->ErrorDescription); } $ids[] = $response->ShipmentDigest; @@ -1661,7 +1681,7 @@ private function requestQuotes(array $packages): array * @param string[] $quoteIds * @return DataObject[] * @throws LocalizedException - * @throws \RuntimeException + * @throws RuntimeException */ private function requestShipments(array $quoteIds): array { @@ -1697,11 +1717,11 @@ private function requestShipments(array $quoteIds): array try { /** @var Element $response */ $response = $this->_xmlElFactory->create(['data' => $httpResponse->getBody()]); - } catch (\Throwable $e) { - throw new \RuntimeException($e->getMessage()); + } catch (Throwable $e) { + throw new RuntimeException($e->getMessage()); } if (isset($response->Error)) { - throw new \RuntimeException((string)$response->Error->ErrorDescription); + throw new RuntimeException((string)$response->Error->ErrorDescription); } else { $shippingLabelContent = (string)$response->ShipmentResults->PackageResults->LabelImage->GraphicImage; $trackingNumber = (string)$response->ShipmentResults->PackageResults->TrackingNumber; @@ -1726,7 +1746,7 @@ private function requestShipments(array $quoteIds): array protected function _doShipmentRequest(DataObject $request) { $this->_prepareShipmentRequest($request); - $result = new \Magento\Framework\DataObject(); + $result = new DataObject(); $rawXmlRequest = $this->_formShipmentRequest($request); $this->setXMLAccessRequest(); $xmlRequest = $this->_xmlAccessRequest . $rawXmlRequest; @@ -1747,14 +1767,14 @@ protected function _doShipmentRequest(DataObject $request) $xmlResponse = $deferredResponse->get()->getBody(); $debugData['result'] = $xmlResponse; $this->_setCachedQuotes($xmlRequest, $xmlResponse); - } catch (\Throwable $e) { + } catch (Throwable $e) { $debugData['result'] = ['code' => $e->getCode(), 'error' => $e->getMessage()]; } } try { $response = $this->_xmlElFactory->create(['data' => $xmlResponse]); - } catch (\Throwable $e) { + } catch (Throwable $e) { $debugData['result'] = ['error' => $e->getMessage(), 'code' => $e->getCode()]; $result->setErrors($e->getMessage()); } @@ -1827,7 +1847,7 @@ public function requestToShipment($request) $labels = $this->requestShipments($quoteIds); } catch (LocalizedException $exception) { return new DataObject(['errors' => [$exception->getMessage()]]); - } catch (\RuntimeException $exception) { + } catch (RuntimeException $exception) { return new DataObject(['errors' => __('Failed to send items')]); } // phpcs:enable @@ -1848,11 +1868,11 @@ public function returnOfShipment($request) /** * Return container types of carrier * - * @param \Magento\Framework\DataObject|null $params + * @param DataObject|null $params * @return array|bool * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ - public function getContainerTypes(\Magento\Framework\DataObject $params = null) + public function getContainerTypes(DataObject $params = null) { if ($params === null) { return $this->_getAllowedContainers($params); @@ -1932,10 +1952,10 @@ public function getContainerTypesFilter() /** * Return delivery confirmation types of carrier * - * @param \Magento\Framework\DataObject|null $params + * @param DataObject|null $params * @return array|bool */ - public function getDeliveryConfirmationTypes(\Magento\Framework\DataObject $params = null) + public function getDeliveryConfirmationTypes(DataObject $params = null) { $countryRecipient = $params != null ? $params->getCountryRecipient() : null; $deliveryConfirmationTypes = [];