Lines 100.00% 9 / 9
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ReadinessCheck 100.00% 9 / 9 100.00% 1 / 1 7 100.00% 1 / 1
 assessSnapshot 100.00% 9 / 9 100.00% 1 / 1 7
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Health;
6
7use Stewart\Runtime\Control\Protocol\Status\RuntimeSnapshot;
8use Stewart\Runtime\Control\Protocol\Status\WorkerStatus;
9use Stewart\Runtime\Lifecycle\ConnectionPhase;
10use Stewart\Runtime\Lifecycle\WorkerPhase;
11
12final readonly class ReadinessCheck
13{
14    public function assessSnapshot(RuntimeSnapshot $snapshot): ReadinessVerdict
15    {
16        $unreadyReasons = [];
17
18        if ($snapshot->connection->phase !== ConnectionPhase::Connected) {
19            $unreadyReasons[] = 'Home Assistant is ' . $snapshot->connection->phase->value;
20        }
21
22        if ($snapshot->workers !== [] && !array_any($snapshot->workers, static fn(WorkerStatus $worker): bool => $worker->phase === WorkerPhase::Live && $worker->ready)) {
23            $unreadyReasons[] = 'no worker is running';
24        }
25
26        foreach ($snapshot->workers as $worker) {
27            if ($worker->phase === WorkerPhase::Quarantined) {
28                $unreadyReasons[] = \sprintf('worker %d is quarantined', $worker->workerId);
29            }
30        }
31
32        return new ReadinessVerdict($unreadyReasons);
33    }
34}