From 436e55f7ca00c39368da000e4f637d1796e349b2 Mon Sep 17 00:00:00 2001 From: "@ChristelLoftus" <christel@bob.co.za> Date: Tue, 3 Sep 2024 11:16:38 +0200 Subject: [PATCH] cleanup --- Block/TrackOrderLink.php | 10 ++++++---- Controller/Tracking/Index.php | 25 +++++-------------------- etc/frontend/di.xml | 5 ----- 3 files changed, 11 insertions(+), 29 deletions(-) diff --git a/Block/TrackOrderLink.php b/Block/TrackOrderLink.php index 54c3c6f..ada8c22 100644 --- a/Block/TrackOrderLink.php +++ b/Block/TrackOrderLink.php @@ -12,11 +12,11 @@ class TrackOrderLink extends Current public function __construct( \Magento\Framework\View\Element\Template\Context $context, ScopeConfigInterface $scopeConfig, - \Magento\Framework\App\DefaultPathInterface $defaultPath, // Add this dependency + \Magento\Framework\App\DefaultPathInterface $defaultPath, array $data = [] ) { $this->scopeConfig = $scopeConfig; - parent::__construct($context, $defaultPath, $data); // Pass it to the parent constructor + parent::__construct($context, $defaultPath, $data); } protected function _toHtml() @@ -27,11 +27,13 @@ class TrackOrderLink extends Current \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); + // Return an empty string if the feature is disabled if (!$isEnabled) { - return ''; // Return an empty string if the feature is disabled + return ''; } - return parent::_toHtml(); // Use the parent class's rendering method + // Use the parent class's rendering method + return parent::_toHtml(); } } diff --git a/Controller/Tracking/Index.php b/Controller/Tracking/Index.php index c3a01ee..78e527f 100644 --- a/Controller/Tracking/Index.php +++ b/Controller/Tracking/Index.php @@ -32,7 +32,7 @@ class Index extends \Magento\Framework\App\Action\Action RedirectFactory $redirectFactory, StoreManagerInterface $storeManager, Curl $curl, - Registry $registry // Add Registry + Registry $registry ) { $this->resultPageFactory = $resultPageFactory; $this->jsonFactory = $jsonFactory; @@ -41,12 +41,13 @@ class Index extends \Magento\Framework\App\Action\Action $this->redirectFactory = $redirectFactory; $this->storeManager = $storeManager; $this->curl = $curl; - $this->registry = $registry; // Assign Registry + $this->registry = $registry; parent::__construct($context); } public function execute() { + // This is only an extra check after the TrackOrderLink block // Check if the "Track My Order" feature is enabled $isEnabled = $this->scopeConfig->isSetFlag( 'carriers/bobgo/enable_track_order', @@ -58,48 +59,32 @@ class Index extends \Magento\Framework\App\Action\Action return $this->redirectFactory->create()->setPath('noroute'); } - $this->logger->info('Page Controller is executed.'); $trackingReference = $this->getRequest()->getParam('order_reference'); - $this->logger->info('Tracking reference:', [$trackingReference]); - $channel = $this->getStoreUrl(); - $this->logger->info('Channel:', [$channel]); - if ($trackingReference) { $trackingUrl = sprintf(UData::TRACKING, $channel, $trackingReference); - - try { $this->curl->get($trackingUrl); $response = $this->curl->getBody(); - $this->logger->info('Response:', [$response]); - $decodedResponse = json_decode($response, true); - $this->logger->info('Decoded Response:', [$decodedResponse]); if (is_array($decodedResponse) && isset($decodedResponse[0])) { $shipmentData = $decodedResponse[0]; // Save data to the registry $this->registry->register('shipment_data', $shipmentData); - $this->logger->info('Shipment data registered in the registry.'); } else { - $this->logger->info('Unexpected response format.'); + // Return early the response is not valid + return $this->resultPageFactory->create(); } - - } catch (\Exception $e) { - $this->logger->info('Track error: ' . $e->getMessage()); - $this->messageManager->addErrorMessage(__('Unable to track the order.')); - } } return $this->resultPageFactory->create(); } - private function getStoreUrl(): string { $url = $this->storeManager->getStore()->getBaseUrl(); diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml index 3814d81..bc1aaeb 100644 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -13,9 +13,4 @@ <argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument> </arguments> </type> - <type name="Magento\Framework\View\Layout"> - <arguments> - <argument name="debug" xsi:type="boolean">true</argument> - </arguments> - </type> </config> -- GitLab