Lines 100.00% 28 / 28
Methods 100.00% 5 / 5
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 connected 100.00% 7 / 7 100.00% 1 / 1 2
 connectionLost 100.00% 5 / 5 100.00% 1 / 1 2
 messageDropped 100.00% 6 / 6 100.00% 1 / 1 3
 buildMqttStatus 100.00% 9 / 9 100.00% 1 / 1 1
18final class MqttConnectionTracker implements MqttLinkListener
19{
20    private ConnectionPhase $phase = ConnectionPhase::Connecting;
21
22    private ?Instant $since = null;
23
24    private int $reconnects = 0;
25
26    private ?MonotonicTime $lostAt = null;
27
28    private ?Duration $lastOutage = null;
29
30    private ?string $lastError = null;
31
32    /** @var array<string, int> */
33    private array $dropsByReason = [];
34
35    private readonly EveryNthOccurrence $dropReports;
36
37    public function __construct(
38        private readonly Clock $clock,
39        private readonly LoggerInterface $logger,
40    ) {
41        $this->dropReports = EveryNthOccurrence::forRepeatedWarnings();
42    }
43
44    public function connected(): void
45    {
46        if ($this->lostAt !== null) {
47            $this->lastOutage = $this->clock->getMonotonicTime()->elapsedSince($this->lostAt);
48            $this->lostAt = null;
49            ++$this->reconnects;
50        }
51
52        $this->phase = ConnectionPhase::Connected;
53        $this->since = $this->clock->getNow();
54        $this->lastError = null;
55    }
56
57    public function connectionLost(string $reason): void
58    {
59        $this->lastError = $reason;
60
61        if ($this->phase === ConnectionPhase::Connected) {
62            $this->phase = ConnectionPhase::Reconnecting;
63            $this->since = $this->clock->getNow();
64            $this->lostAt = $this->clock->getMonotonicTime();
65        }
66    }
67
68    // Status and metrics hold the exact counts, so a long outage logs its drops sampled rather than one by one.
69    public function messageDropped(MqttDropReason $reason, string $topic): void
70    {
71        $dropped = $this->dropsByReason[$reason->value] = ($this->dropsByReason[$reason->value] ?? 0) + 1;
72        $context = ['topic' => $topic, 'dropped' => $dropped];
73
74        if ($reason === MqttDropReason::GaveUp) {
75            $this->logger->error($reason->describeDrop(), $context);
76        } elseif ($this->dropReports->includesOccurrence($dropped)) {
77            $this->logger->warning($reason->describeDrop(), $context);
78        }
79    }
80
81    public function buildMqttStatus(int $outboundQueued): MqttStatus
82    {
83        return new MqttStatus(
84            phase: $this->phase,
85            since: $this->since,
86            reconnects: $this->reconnects,
87            lastOutage: $this->lastOutage,
88            lastError: $this->lastError,
89            dropped: array_map(fn(MqttDropReason $reason): MqttDropCount => new MqttDropCount($reason, $this->dropsByReason[$reason->value] ?? 0), MqttDropReason::cases()),
90            outboundQueued: $outboundQueued,
91        );
92    }
93}