rosavox/lib/alveolata/log/output-implementation-email.php
2025-05-23 07:33:29 +00:00

139 lines
2.5 KiB
PHP

<?php
namespace alveolata\log;
// require_once(DIR_ALVEOLATA . '/definitions.php');
require_once(DIR_ALVEOLATA . '/string/functions.php');
require_once(DIR_ALVEOLATA . '/list/functions.php');
require_once(DIR_ALVEOLATA . '/report/functions.php');
require_once(DIR_ALVEOLATA . '/log/output-interface.php');
require_once(DIR_ALVEOLATA . '/email/functions.php');
/**
* @author Christian Fraß <frass@greenscale.de>
*/
class implementation_email implements interface_output
{
/**
* @var $auth {
* record<
* host:string,
* port:integer,
* authtype:string,
* username:string,
* password:string
* >
* }
*/
private $auth;
/**
* @var array {list<string>}
*/
private $receivers;
/**
* @var string
*/
private $sender;
/**
* @var array {list<string>}
*/
private $tags;
/**
* @var string
*/
private $implementation;
/**
* @author Christian Fraß <frass@greenscale.de>
*/
public function __construct(
array $auth,
array $receivers,
string $sender,
array $tags,
string $implementation
)
{
$this->auth = $auth;
$this->receivers = $receivers;
$this->sender = $sender;
$this->tags = $tags;
$this->implementation = $implementation;
}
/**
* @implementation
* @author Christian Fraß <frass@greenscale.de>
*/
public function process(
int $level,
\alveolata\report\struct_report $report
) : void
{
$subject = \alveolata\string\coin(
'{{tags}} {{level}}: {{incident}}',
[
'tags' => \alveolata\string\join(
\alveolata\list_\map(
$this->tags,
function (string $tag): string {
return \alveolata\string\coin('[{{tag}}]', ['tag' => $tag]);
}
),
' '
),
'level' => output_translate_level($level),
'incident' => $report->incident,
]
);
$body_plain = \alveolata\string\coin(
"<{{timestamp}}>\n{{details}}",
[
'timestamp' => date('Y-m-d|H:i:s', $report->timestamp),
'details' => implode(
"\n",
\alveolata\list_\map(
array_keys($report->details),
function ($key) use ($report) {
$value = $report->details[$key];
return \alveolata\string\coin(
'{{key}}: {{value}}',
[
'key' => $key,
'value' => json_encode($value),
]
);
}
)
),
]
);
\alveolata\email\send(
$this->auth,
[
'to' => $this->receivers,
'from' => $this->sender,
'subject' => $subject,
'body_plain' => $body_plain,
],
[
'implementation' => $this->implementation
]
);
}
}
?>