6700ff145d
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
64 lines
1.8 KiB
PHP
64 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXC\Service;
|
|
|
|
use KTXC\Stores\FirewallStore;
|
|
|
|
final class FirewallStatusService
|
|
{
|
|
public function __construct(private readonly FirewallStore $store)
|
|
{
|
|
}
|
|
|
|
public function tenantMetrics(string $tenantId, ?string $since = null): array
|
|
{
|
|
$sinceDate = self::date($since);
|
|
return [
|
|
'tenantId' => $tenantId,
|
|
'blockedRequests' => $this->store->countBlockedRequests($tenantId, $sinceDate),
|
|
'since' => $sinceDate?->format(\DateTimeInterface::ATOM),
|
|
];
|
|
}
|
|
|
|
public function systemMetrics(?string $tenantId = null, ?string $since = null): array
|
|
{
|
|
if ($tenantId !== null && ($tenantId === '' || strlen($tenantId) > 128)) {
|
|
throw new \InvalidArgumentException('Invalid tenant filter.');
|
|
}
|
|
$sinceDate = self::date($since);
|
|
return [
|
|
'tenantId' => $tenantId,
|
|
'blockedRequests' => $this->store->countSystemBlockedRequests($tenantId, $sinceDate),
|
|
'since' => $sinceDate?->format(\DateTimeInterface::ATOM),
|
|
];
|
|
}
|
|
|
|
public function maintenanceStatus(): array
|
|
{
|
|
return $this->store->maintenanceStatus() ?? [
|
|
'status' => 'never_run',
|
|
'startedAt' => null,
|
|
'completedAt' => null,
|
|
'result' => null,
|
|
'error' => null,
|
|
];
|
|
}
|
|
|
|
private static function date(?string $value): ?\DateTimeImmutable
|
|
{
|
|
if ($value === null) {
|
|
return null;
|
|
}
|
|
if ($value === '' || strlen($value) > 255) {
|
|
throw new \InvalidArgumentException('Invalid since date filter.');
|
|
}
|
|
try {
|
|
return new \DateTimeImmutable($value);
|
|
} catch (\Exception) {
|
|
throw new \InvalidArgumentException('Invalid since date filter.');
|
|
}
|
|
}
|
|
}
|