- Added some more type checking

This commit is contained in:
Dave Mc Nicoll 2023-11-01 11:29:08 -04:00
parent 951c93fc63
commit 4484f1c997
6 changed files with 16 additions and 10 deletions

View File

@ -22,7 +22,7 @@ namespace Negundo\Client {
static::$instances[] = $this; static::$instances[] = $this;
} }
public function pushData(...$content) : ? object public function pushData(...$content) : object|null|bool
{ {
$data = $this->dumpHandler->dumpData(...$content); $data = $this->dumpHandler->dumpData(...$content);

View File

@ -55,7 +55,7 @@ abstract class Handler {
}); });
} }
public function pushData(\Throwable $ex) : ? object public function pushData(\Throwable $ex) : null|object|bool
{ {
// Make sure not to spam the server if an ErrorMessage or Exception was already sent (like inside a loop) // Make sure not to spam the server if an ErrorMessage or Exception was already sent (like inside a loop)
$exceptionHash = $this->exceptionHandler->hash($ex); $exceptionHash = $this->exceptionHandler->hash($ex);

View File

@ -25,7 +25,7 @@ namespace Negundo\Client {
static::$instances[] = $this; static::$instances[] = $this;
} }
public function newReport(string $message, ? string $title = null, ? array $data = []) : ? object public function newReport(string $message, ? string $title = null, ? array $data = []) : object|null|bool
{ {
$report = $this->taskHandler->sendReport($message, $title, $data); $report = $this->taskHandler->sendReport($message, $title, $data);
@ -45,9 +45,13 @@ namespace Negundo\Client {
namespace { namespace {
if (! function_exists('ntask') ) { if (! function_exists('ntask') ) {
function ntask(string $message, ? string $title = null, ? array $data) { function ntask(string $message, ? string $title = null, ? array $data = null) {
foreach (\Negundo\Client\Task::$instances as $instance) { foreach (\Negundo\Client\Task::$instances as $instance) {
$instance->newReport($message, $title, $data); $sent = $instance->newReport($message, $title, $data);
if (! $sent ) {
throw new \Exception(sprintf('Could not send report titled `%s`.', $title));
}
} }
} }
} }

View File

@ -10,7 +10,7 @@ class Curl implements TransportInterface {
public $headers = []; public $headers = [];
public function push(string $url, array $data) : ? object public function push(string $url, array $data) : object|null|bool
{ {
$ch = curl_init(); $ch = curl_init();
@ -22,7 +22,9 @@ class Curl implements TransportInterface {
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout * 200); curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout * 200);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ( ( false === curl_exec($ch) ) && $this->throwErrors ) { $exec = curl_exec($ch);
if ( ( false === $exec ) && $this->throwErrors ) {
$errno = curl_errno($ch); $errno = curl_errno($ch);
$errors = array_filter([ curl_error($ch) , static::CURL_ERROR[$errno] ?? null ]); $errors = array_filter([ curl_error($ch) , static::CURL_ERROR[$errno] ?? null ]);
@ -31,7 +33,7 @@ class Curl implements TransportInterface {
curl_close($ch); curl_close($ch);
return null; return $exec;
} }
const CURL_ERROR = [ const CURL_ERROR = [

View File

@ -12,7 +12,7 @@ class GuzzleClient implements TransportInterface {
public array $headers = []; public array $headers = [];
public function push(string $url, array $data) : ? object public function push(string $url, array $data) : object|null|bool
{ {
return ( new Client([ return ( new Client([
'timeout' => $this->timeout, 'timeout' => $this->timeout,

View File

@ -3,5 +3,5 @@
namespace Negundo\Client\Transport; namespace Negundo\Client\Transport;
interface TransportInterface { interface TransportInterface {
public function push(string $url, array $data) : ? object; public function push(string $url, array $data) : object|null|bool;
} }