![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/Dhl/Preference/Model/ |
<?php /** * Copyright (c) 2021 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Krzysztof Majkowski <[email protected]> <[email protected]> */ namespace Cnc\Dhl\Preference\Model; use Magento\Framework\App\ObjectManager; use Magento\Framework\App\ProductMetadataInterface; use Magento\Framework\Async\CallbackDeferred; use Magento\Framework\HTTP\AsyncClient\HttpResponseDeferredInterface; use Magento\Framework\HTTP\AsyncClient\Request; use Magento\Framework\HTTP\AsyncClientInterface; use Magento\Framework\Xml\Security; use Magento\Shipping\Model\Rate\Result\ProxyDeferredFactory; class Carrier extends \Magento\Dhl\Model\Carrier { /** * @var AsyncClientInterface */ private $httpClient; /** * @var ProxyDeferredFactory */ private $proxyDeferredFactory; public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory, \Psr\Log\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\Shipping\Helper\Carrier $carrierHelper, \Magento\Framework\Stdlib\DateTime\DateTime $coreDate, \Magento\Framework\Module\Dir\Reader $configReader, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Stdlib\StringUtils $string, \Magento\Framework\Math\Division $mathDivision, \Magento\Framework\Filesystem\Directory\ReadFactory $readFactory, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Framework\HTTP\LaminasClientFactory $httpClientFactory, array $data = [], \Magento\Dhl\Model\Validator\XmlValidator $xmlValidator = null, ProductMetadataInterface $productMetadata = null, ?AsyncClientInterface $httpClient = null, ?ProxyDeferredFactory $proxyDeferredFactory = null ) { parent::__construct( $scopeConfig, $rateErrorFactory, $logger, $xmlSecurity, $xmlElFactory, $rateFactory, $rateMethodFactory, $trackFactory, $trackErrorFactory, $trackStatusFactory, $regionFactory, $countryFactory, $currencyFactory, $directoryData, $stockRegistry, $carrierHelper, $coreDate, $configReader, $storeManager, $string, $mathDivision, $readFactory, $dateTime, $httpClientFactory, $data, $xmlValidator, $productMetadata, $httpClient, $proxyDeferredFactory ); if ($this->getConfigData('content_type') == self::DHL_CONTENT_TYPE_DOC) { $this->_freeMethod = 'free_method_doc'; } $this->httpClient = $httpClient ?? ObjectManager::getInstance()->get(AsyncClientInterface::class); $this->proxyDeferredFactory = $proxyDeferredFactory ?? ObjectManager::getInstance()->get(ProxyDeferredFactory::class); } /** * @overridden * * Overridden because native module use old code: * (string)$this->getConfigData('gateway_url') * * @return \Magento\Framework\Model\AbstractModel|\Magento\Shipping\Model\Rate\Result|\Magento\Shipping\Model\Rate\Result\ProxyDeferred */ protected function _getQuotes() { $responseBodies = []; /** @var HttpResponseDeferredInterface[][] $deferredResponses */ $deferredResponses = []; $requestXml = $this->_buildQuotesRequestXml(); for ($offset = 0; $offset <= self::UNAVAILABLE_DATE_LOOK_FORWARD; $offset++) { $date = date(self::REQUEST_DATE_FORMAT, strtotime($this->_getShipDate() . " +{$offset} days")); $this->_setQuotesRequestXmlDate($requestXml, $date); $request = $requestXml->asXML(); $responseBody = $this->_getCachedQuotes($request); if ($responseBody === null) { $deferredResponses[] = [ 'deferred' => $this->httpClient->request( new Request( (string) $this->getGatewayURL(), Request::METHOD_POST, ['Content-Type' => 'application/xml'], utf8_encode($request) ) ), 'date' => $date, 'request' => $request ]; } else { $responseBodies[] = [ 'body' => $responseBody, 'date' => $date, 'request' => $request, 'from_cache' => true ]; } } return $this->proxyDeferredFactory->create( [ 'deferred' => new CallbackDeferred( function () use ($deferredResponses, $responseBodies) { //Loading rates not found in cache foreach ($deferredResponses as $deferredResponseData) { $responseBodies[] = [ 'body' => $deferredResponseData['deferred']->get()->getBody(), 'date' => $deferredResponseData['date'], 'request' => $deferredResponseData['request'], 'from_cache' => false ]; } return $this->processQuotesResponses($responseBodies); } ) ] ); } /** * @overridden * @inheritDoc * * Not changed at all. */ private function processQuotesResponses(array $responsesData) { usort( $responsesData, function (array $a, array $b): int { return $a['date'] <=> $b['date']; } ); /** @var string $lastResponse */ $lastResponse = ''; //Processing different dates foreach ($responsesData as $responseData) { $debugPoint = []; $debugPoint['request'] = $this->filterDebugData($responseData['request']); $debugPoint['response'] = $this->filterDebugData($responseData['body']); $debugPoint['from_cache'] = $responseData['from_cache']; $unavailable = false; try { //Getting availability $bodyXml = $this->_xmlElFactory->create(['data' => $responseData['body']]); $code = $bodyXml->xpath('//GetQuoteResponse/Note/Condition/ConditionCode'); if (isset($code[0]) && (int)$code[0] == self::CONDITION_CODE_SERVICE_DATE_UNAVAILABLE) { $debugPoint['info'] = sprintf( __("DHL service is not available at %s date"), $responseData['date'] ); $unavailable = true; } } catch (\Throwable $exception) { //Failed to read response $unavailable = true; $this->_errors[$exception->getCode()] = $exception->getMessage(); } if ($unavailable) { //Cannot get rates. $this->_debug($debugPoint); break; } //Caching rates $this->_setCachedQuotes($responseData['request'], $responseData['body']); $this->_debug($debugPoint); //Will only process rates available for the latest date possible. $lastResponse = $responseData['body']; } return $this->_parseResponse($lastResponse); } /** * @overridden * @inheritDoc * Not changed at all. * * Get the gateway URL * * @return string */ private function getGatewayURL(): string { if ($this->getConfigData('sandbox_mode')) { return (string)$this->getConfigData('sandbox_url'); } else { return (string)$this->getConfigData('gateway_url'); } } }