- First commit of Negundo

This commit is contained in:
mcndave committed 2023-03-29 13:52:15 +00:00
commit 69ed176b1c
18 files changed
+846

No files matched your search

+54
View File
@@ -0,0 +1,54 @@
<?php
namespace Negundo\Client {
class Task {
public static /* array */ $instances = [];
protected /*array*/ $sent = [];
protected /* TransportInterface */ $transport;
protected /* Util\TaskHandler */ $taskHandler;
protected /* SoftwareConfig */ $config;
public function __construct(SoftwareConfig $config, ? DataInterface $dataManipulator = null, Transport\TransportInterface $transport = null)
{
$this->config = $config;
$this->transport = $transport ?: new Transport\Curl();
$this->taskHandler = new Util\TaskHandler($dataManipulator);
static::$instances[] = $this;
}
public function newReport(string $message, ? string $title = null, ? array $data = []) : ? object
{
$report = $this->taskHandler->sendReport($message, $title, $data);
// Make sure not to spam the server if an ErrorMessage or Exception was already sent (like inside a loop)
$dumpHash = $this->taskHandler->hash($report);
if ( $this->sent[$dumpHash] ?? false ) {
return null;
}
$this->sent[$dumpHash] = true;
return $this->transport->push($this->config->url('task/report'), $report);
}
}
}
namespace {
if (! function_exists('ntask') ) {
function ntask(string $message, ? string $title = null, ? array $data) {
foreach (\Negundo\Task::$instances as $instance) {
$instance->newReport($message, $title, $data);
}
}
}
}