From 38df94fc2a0bbfe6d8106ada91c71767fb03eca2 Mon Sep 17 00:00:00 2001
From: Dave Mc Nicoll <dave.mcnicoll@cslsj.qc.ca>
Date: Fri, 1 Nov 2024 16:10:18 -0400
Subject: [PATCH] - WIP on a new TaksReport object

---
 src/Task.php             | 15 ++++++---
 src/Task/StatusEnum.php  |  4 ++-
 src/Task/TaskReport.php  | 72 ++++++++++++++++++++++++++++++++++++++++
 src/Transport/Curl.php   |  9 +++--
 src/Util/TaskHandler.php |  3 +-
 5 files changed, 94 insertions(+), 9 deletions(-)
 create mode 100644 src/Task/TaskReport.php

diff --git a/src/Task.php b/src/Task.php
index e42cc46..592ac38 100644
--- a/src/Task.php
+++ b/src/Task.php
@@ -25,9 +25,9 @@ namespace Negundo\Client {
             static::$instances[] = $this;
         }
 
-        public function newReport(string $message, ? string $title = null, ? array $data = []) : object|null|bool
+        public function newReport(string $message, ? string $title = null, ? array $data = [], ? array $events = []) : object|null|bool
         {
-            $report = $this->taskHandler->sendReport($message, $title, $data);
+            $report = $this->taskHandler->sendReport($message, $title, $data, $events);
 
             // Make sure not to spam the server if an ErrorMessage or Exception was already sent (like inside a loop)
             $dumpHash = $this->taskHandler->hash($report);
@@ -44,15 +44,22 @@ namespace Negundo\Client {
 }
 
 namespace {
+
+    use Negundo\Client\Task\TaskReport;
+
     if (! function_exists('ntask') ) {
-        function ntask(string $message, ? string $title = null, ? array $data = null, ? Negundo\Client\Task\StatusEnum $status = null) {
+        function ntask(string $message, ? string $title = null, ? array $data = null, ? Negundo\Client\Task\StatusEnum $status = null, array $events = []) {
             foreach (\Negundo\Client\Task::$instances as $instance) {
-                $sent = $instance->newReport($message, $title, $data);
+                $sent = $instance->newReport($message, $title, $data, $events);
 
                 if (! $sent ) {
                     throw new \Exception(sprintf('Could not send report titled `%s`.', $title));
                 }
             }
         }
+
+        function nreport(TaskReport $report) {
+            ntask($report->getMessage(), $report->getTitle(), $report->getData(), $report->getStatus(), $report->getEvents());
+        }
     }
 }
\ No newline at end of file
diff --git a/src/Task/StatusEnum.php b/src/Task/StatusEnum.php
index 4c65a9e..b1d0351 100644
--- a/src/Task/StatusEnum.php
+++ b/src/Task/StatusEnum.php
@@ -4,7 +4,9 @@ namespace Negundo\Client\Task;
 
 enum StatusEnum : string
 {
-    case Completed = "completed";
     case Failed = "failed";
     case Warning = "warning";
+    case Completed = "completed";
+    case Empty = "empty";
+    case NothingToDo = "nothing-to-do";
 }
\ No newline at end of file
diff --git a/src/Task/TaskReport.php b/src/Task/TaskReport.php
new file mode 100644
index 0000000..e6fc69d
--- /dev/null
+++ b/src/Task/TaskReport.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace Negundo\Client\Task;
+
+class TaskReport
+{
+    protected array $events = [];
+
+    public function __construct(
+        public string|array $message,
+        public string $title,
+        public ? StatusEnum $status = null,
+        public array $data = [],
+    ) {}
+
+    public function addData(string $name, array $data) : static
+    {
+        $this->data[$name][] = $data;
+
+        return $this;
+    }
+
+    public function addEvent(string $key, ? StatusEnum $status = null, array $data) : static
+    {
+        $this->events[] = [
+            'key' => $key,
+            'status' => $status ? $status->value : null,
+            'data' => $data
+        ];
+
+        return $this;
+    }
+
+    public function addMessage(string $message) : static
+    {
+        if ( is_string($this->message) ) {
+            $this->message = [
+                $this->message, $message
+            ];
+        }
+        else {
+            $this->message[] = $message;
+        }
+
+        return $this;
+    }
+
+    public function getMessage() : string
+    {
+        return implode(PHP_EOL, (array) $this->message);
+    }
+
+    public function getTitle() : string
+    {
+        return $this->title;
+    }
+
+    public function getData() : array
+    {
+        return $this->data;
+    }
+
+    public function getEvents() : array
+    {
+        return $this->events;
+    }
+
+    public function getStatus() : StatusEnum
+    {
+        return $this->status;
+    }
+}
\ No newline at end of file
diff --git a/src/Transport/Curl.php b/src/Transport/Curl.php
index 8d71533..4e81d34 100644
--- a/src/Transport/Curl.php
+++ b/src/Transport/Curl.php
@@ -4,8 +4,8 @@ namespace Negundo\Client\Transport;
 
 class Curl implements TransportInterface {
     
-    public $timeout = 2;
-    
+    public $timeout = 100;
+
     public $throwErrors = true;
 
     public $verifySsl = false;
@@ -39,7 +39,10 @@ class Curl implements TransportInterface {
                 throw new \Exception(sprintf("HTTP code received : $code with page content : %s", $exec));
             }
         }
-
+        if ($_GET['dev'] ?? false) {
+            echo($exec);
+            die();
+        }
         curl_close($ch);
 
         return $exec;
diff --git a/src/Util/TaskHandler.php b/src/Util/TaskHandler.php
index e95e93c..e88b064 100644
--- a/src/Util/TaskHandler.php
+++ b/src/Util/TaskHandler.php
@@ -13,7 +13,7 @@ class TaskHandler {
         $this->dataManipulator = $dataManipulator;
     }
 
-    public function sendReport(string $message, ? string $title = null, array $data = []) : array
+    public function sendReport(string $message, ? string $title = null, array $data = [], array $events = []) : array
     {
         $backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
         array_shift($backtrace);
@@ -36,6 +36,7 @@ class TaskHandler {
                 'request_body' => json_encode($_POST),
                 'remote_addr' => $_SERVER['REMOTE_ADDR'] ?? null,
             ],
+            'events' => $events,
         ];
 
         if ( $this->dataManipulator ?? false ) {