Lines 75.00% 12 / 16
Methods 60.00% 6 / 10
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 stateChanged 100.00% 1 / 1 100.00% 1 / 1 1
 eventFired 100.00% 2 / 2 100.00% 1 / 1 1
 triggerFired 0.00% 0 / 1 0.00% 0 / 1 2
 triggerRejected 0.00% 0 / 1 0.00% 0 / 1 2
 exposedEntitySynced 0.00% 0 / 1 0.00% 0 / 1 2
 exposedEntityCommanded 0.00% 0 / 1 0.00% 0 / 1 2
 connectionLost 100.00% 3 / 3 100.00% 1 / 1 1
 reconnected 100.00% 4 / 4 100.00% 1 / 1 1
 connectionFailed 100.00% 1 / 1 100.00% 1 / 1 1
23final readonly class BrokerHaEvents implements HaSessionListener
24{
25    public function __construct(
26        private HaSession $session,
27        private EventRouter $router,
28        private WorkerSlotRegistry $slots,
29        private ConnectionTracker $connection,
30        private BrokerRun $run,
31        private TriggerRejections $triggerRejections,
32        private RegistryRefresher $registryRefresher,
33        private ExposureCommandRouter $commandRouter,
34    ) {}
35
36    public function stateChanged(StateChange $change): void
37    {
38        $this->router->broadcastStateChange($change);
39    }
40
41    public function eventFired(HaEvent $event): void
42    {
43        $this->registryRefresher->scheduleRefreshFor($event);
44        $this->router->routeEvent($event);
45    }
46
47    public function triggerFired(TriggerSpec $spec, TriggerEvent $event): void
48    {
49        $this->router->routeTrigger($spec, $event);
50    }
51
52    public function triggerRejected(TriggerSpec $spec, string $reason): void
53    {
54        $this->triggerRejections->refuseSubscribers($spec, $reason);
55    }
56
57    public function exposedEntitySynced(ExposedEntitySync $sync): void
58    {
59        $this->slots->findHandleForWorker($sync->owner)?->send(new ExposedEntitySynced(ResourceScope::forApp($sync->appId), $sync->key, $sync->snapshot));
60    }
61
62    public function exposedEntityCommanded(ExposedEntityCommand $command): void
63    {
64        $this->commandRouter->routeCommand($command);
65    }
66
67    public function connectionLost(string $reason, Instant $lostAt): void
68    {
69        $lost = new HaConnectionLost($lostAt, $reason);
70        $this->connection->markReconnecting($lost);
71        $this->slots->broadcast($lost);
72    }
73
74    public function reconnected(Duration $outage): void
75    {
76        $snapshot = $this->session->snapshotStateCache();
77        $this->connection->markRestored($outage);
78
79        // The registry goes first so a resynced change is matched against the current areas and labels.
80        $this->slots->broadcast($this->session->snapshotRegistry()->toRegistrySnapshot());
81        $this->slots->broadcast(new StateResynced($snapshot->states, $snapshot->revision, $outage));
82    }
83
84    public function connectionFailed(Throwable $error): void
85    {
86        $this->run->stopWithError('Home Assistant rejected the connection', $error);
87    }
88}