negundo-client/src/Util/TaskHandler.php

57 lines
1.7 KiB
PHP

<?php
namespace Negundo\Client\Util;
use Negundo\Client\DataInterface;
class TaskHandler {
public /*DataInterface*/ $dataManipulator;
public function __construct(DataInterface $dataManipulator = null)
{
$this->dataManipulator = $dataManipulator;
}
public function sendReport(string $message, ? string $title = null, array $data = []) : array
{
$backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
array_shift($backtrace);
array_shift($backtrace);
$trace = $backtrace[0] ?? [
"line" => -1,
"file" => "unknown",
];
$post = [
'file' => $trace['file'],
'line' => $trace['line'],
'title' => $title ?: substr($trace['file'], 0, 255),
'message' => $message,
'url' => ( ( 'on' === ($_SERVER['HTTPS'] ?? false) ) ? 'https' : 'http' ) . '://' . ( $_SERVER['HTTP_HOST'] ?? "") . ( $_SERVER["REQUEST_URI"] ?? "" ),
'data' => $data + [
'sent_at' => date('Y-m-d H:i:s'),
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? null,
'request_body' => json_encode($_POST),
'remote_addr' => $_SERVER['REMOTE_ADDR'] ?? null,
],
];
if ( $this->dataManipulator ?? false ) {
$post['data'] += $this->dataManipulator->getData();
$this->dataManipulator->run($post);
}
$post['data'] = json_encode($post['data'] ?? null);
return $post;
}
public function hash(array $content) : string
{
return md5(implode('-', [ $content['file'], $content['line'], $content['title'] ]));
}
}