Lines
100.00%
10 / 10
Methods
100.00%
2 / 2
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| handleRequest | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 4 | ||
| 17 | final readonly class ProbeRequestHandler implements RequestHandler | |
| 18 | { | |
| 19 | private const array PROBE_PATHS = ['/healthz' => ProbeKind::Liveness, '/readyz' => ProbeKind::Readiness]; | |
| 20 | ||
| 21 | private const array REPORT_HEADERS = ['content-type' => 'application/json', 'cache-control' => 'no-store']; | |
| 22 | ||
| 23 | public function __construct( | |
| 24 | private ProbeReporter $reporter, | |
| 25 | private ProbeReportCodec $codec, | |
| 26 | private LoggerInterface $logger, | |
| 27 | ) {} | |
| 28 | ||
| 29 | public function handleRequest(Request $request): Response | |
| 30 | { | |
| 31 | $kind = self::PROBE_PATHS[$request->getUri()->getPath()] ?? null; | |
| 32 | ||
| 33 | if ($kind === null) { | |
| 34 | return new Response(HttpStatus::NOT_FOUND); | |
| 35 | } | |
| 36 | ||
| 37 | try { | |
| 38 | $report = $this->reporter->reportProbe($kind); | |
| 39 | $body = $this->codec->encodeReport($report); | |
| 40 | } catch (Throwable $e) { | |
| 41 | $this->logger->error('Could not answer the probe', ['probe' => $kind->value, 'exception' => $e]); | |
| 42 | ||
| 43 | return new Response(HttpStatus::INTERNAL_SERVER_ERROR); | |
| 44 | } | |
| 45 | ||
| 46 | return new Response($report->status->isHealthy() ? HttpStatus::OK : HttpStatus::SERVICE_UNAVAILABLE, self::REPORT_HEADERS, $body); | |
| 47 | } | |
| 48 | } |