- WIP on a new TaksReport object

This commit is contained in:
Dave Mc Nicoll 2024-11-01 16:10:18 -04:00
parent 5a737c06c4
commit 38df94fc2a
5 changed files with 94 additions and 9 deletions

View File

@ -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());
}
}
}

View File

@ -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";
}

72
src/Task/TaskReport.php Normal file
View File

@ -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;
}
}

View File

@ -4,7 +4,7 @@ namespace Negundo\Client\Transport;
class Curl implements TransportInterface {
public $timeout = 2;
public $timeout = 100;
public $throwErrors = true;
@ -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;

View File

@ -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 ) {