Lines 100.00% 6 / 6
Functions and Methods 100.00% 4 / 4
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ConnectionTracker 100.00% 6 / 6 100.00% 4 / 4 4 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 markConnected 100.00% 1 / 1 100.00% 1 / 1 1
 markReconnecting 100.00% 2 / 2 100.00% 1 / 1 1
 markRestored 100.00% 2 / 2 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Broker;
6
7use Stewart\Contracts\Time\Clock;
8use Stewart\Contracts\Time\Duration;
9use Stewart\Runtime\Control\Protocol\Status\ConnectionState;
10use Stewart\Runtime\Ipc\Message\HaConnectionLost;
11use Stewart\Runtime\Lifecycle\ConnectionPhase;
12
13final class ConnectionTracker
14{
15    public private(set) ConnectionState $state;
16
17    public private(set) ?HaConnectionLost $lastLoss = null;
18
19    public function __construct(private readonly Clock $clock)
20    {
21        $this->state = new ConnectionState();
22    }
23
24    public function markConnected(): void
25    {
26        $this->state = new ConnectionState(ConnectionPhase::Connected, $this->clock->getNow());
27    }
28
29    public function markReconnecting(HaConnectionLost $loss): void
30    {
31        $this->lastLoss = $loss;
32        $this->state = new ConnectionState(ConnectionPhase::Reconnecting, $this->clock->getNow(), $this->state->reconnects, $this->state->lastOutage);
33    }
34
35    public function markRestored(Duration $outage): void
36    {
37        $this->lastLoss = null;
38        $this->state = new ConnectionState(ConnectionPhase::Connected, $this->clock->getNow(), $this->state->reconnects + 1, $outage);
39    }
40}