From 2c15955c4c6c7cbb498c5da8cc61af88ae731871 Mon Sep 17 00:00:00 2001
From: Gundo Sifhufhi <sifhufhisg@gmail.com>
Date: Tue, 20 Dec 2022 15:22:06 +0200
Subject: [PATCH] carrier model refactoring

---
 .idea/php.xml                                 |  4 ++
 .../Model/Carrier/Customshipping.php          | 31 ------------
 .../Block/Tracking/PopUpDeliveryDate.php      | 50 +++++++++++++++++++
 3 files changed, 54 insertions(+), 31 deletions(-)
 create mode 100644 .idea/php.xml
 create mode 100644 uafrica/Customshipping/Plugin/Block/Tracking/PopUpDeliveryDate.php

diff --git a/.idea/php.xml b/.idea/php.xml
new file mode 100644
index 0000000..b4bbf1e
--- /dev/null
+++ b/.idea/php.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="PhpProjectSharedConfiguration" php_language_level="7.0" />
+</project>
\ No newline at end of file
diff --git a/uafrica/Customshipping/Model/Carrier/Customshipping.php b/uafrica/Customshipping/Model/Carrier/Customshipping.php
index 4017aab..b761996 100644
--- a/uafrica/Customshipping/Model/Carrier/Customshipping.php
+++ b/uafrica/Customshipping/Model/Carrier/Customshipping.php
@@ -115,37 +115,6 @@ class Customshipping extends AbstractCarrier implements CarrierInterface
         return true;
     }
 
-//    /**
-//     * Obtain Tracking Information from uAfrica/Bobgo
-//     * @param string $trackings
-//     */
-//    public function getTrackingInfo($trackings)
-//    {
-//       // $result = $this->_trackFactory->create();
-//       // $tracking = explode(',', $trackings);
-//        // Get Tracking Results From uafrica API
-//        $this->curl->get("https://api.dev.ship.uafrica.com/tracking?channel=localhost&tracking_reference=UADPCTGF");
-//
-//        $result = $this->curl->getBody();
-//
-//        $bobGo = \Safe\json_decode($result, true);
-////        $result->setUrl('https://api.dev.ship.uafrica.com/tracking?channel=localhost&tracking_reference=UADPCTGF');
-////        $result->setTracking($trackings);
-////        $result->setCarrierTitle($this->getConfigData('title'));
-//            $track = $this->jsonFactory->create()->setData($bobGo);
-//
-//            return $track;
-//    }
-//    /**
-//     * Get tracking information
-//     *
-//     */
-//
-//    public function getTracking()
-//    {
-//        return $this->getTrackingInfo();
-//    }
-
     /**
      * Collect and get rates for storefront
      *
diff --git a/uafrica/Customshipping/Plugin/Block/Tracking/PopUpDeliveryDate.php b/uafrica/Customshipping/Plugin/Block/Tracking/PopUpDeliveryDate.php
new file mode 100644
index 0000000..e605536
--- /dev/null
+++ b/uafrica/Customshipping/Plugin/Block/Tracking/PopUpDeliveryDate.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace uafrica\Customshipping\Plugin\Block\Tracking;
+
+use Magento\Shipping\Block\Tracking\Popup;
+use Magento\Shipping\Model\Tracking\Result\Status;
+use Magento\Shiiping\Model\Carrier;
+
+/*
+ * Plugin to update delivery date value in case if UAfrica is a carrier used
+ */
+class PopupDeliveryDate
+{
+    /**
+     * Show only date for expected delivery in case if UAfrica is a carrier
+     *
+     * @param Popup $subject
+     * @param string $result
+     * @param string $date
+     * @param string $time
+     * @return string
+     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+     */
+    public function afterFormatDeliveryDateTime(Popup $subject, $result, $date, $time)
+    {
+        if ($this->getCarrier($subject) === Carrier::CODE) {
+            $result = $subject->formatDeliveryDate($date);
+        }
+        return $result;
+    }
+
+    /**
+     * Retrieve carrier name from tracking info
+     *
+     * @param Popup $subject
+     * @return string
+     */
+    private function getCarrier(Popup $subject): string
+    {
+        foreach ($subject->getTrackingInfo() as $trackingData) {
+            foreach ($trackingData as $trackingInfo) {
+                if ($trackingInfo instanceof Status) {
+                    $carrier = $trackingInfo->getCarrier();
+                    return $carrier;
+                }
+            }
+        }
+        return '';
+    }
+}
-- 
GitLab