Lines 83.33% 5 / 6
Functions and Methods 66.66% 2 / 3
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
HaConnectionLostHandler 83.33% 5 / 6 66.66% 2 / 3 4.07 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 handledMessageClass 100.00% 1 / 1 100.00% 1 / 1 1
 handle 75.00% 3 / 4 0.00% 0 / 1 2.06
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Worker\Message;
6
7use Psr\Log\LoggerInterface;
8use Stewart\Contracts\Connection\ConnectionLost;
9use Stewart\Runtime\Dispatch\LocalDispatcher;
10use Stewart\Runtime\Ipc\Message\BrokerMessage;
11use Stewart\Runtime\Ipc\Message\HaConnectionLost;
12use Stewart\Runtime\Worker\ConnectionStatus;
13
14/** @implements BrokerMessageHandler<HaConnectionLost> */
15final readonly class HaConnectionLostHandler implements BrokerMessageHandler
16{
17    public function __construct(
18        private ConnectionStatus $connection,
19        private LocalDispatcher $dispatcher,
20        private LoggerInterface $logger,
21    ) {}
22
23    public function handledMessageClass(): string
24    {
25        return HaConnectionLost::class;
26    }
27
28    /** @param HaConnectionLost $message */
29    public function handle(BrokerMessage $message): void
30    {
31        if (!$this->connection->markLost()) {
32            return;
33        }
34
35        $this->logger->warning('Lost Home Assistant; state reads are stale until it is back', ['reason' => $message->reason]);
36        $this->dispatcher->dispatchConnection(new ConnectionLost($message->lostAt, $message->reason));
37    }
38}