Lines 88.88% 24 / 27
Functions and Methods 80.00% 8 / 10
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AppTracer 88.88% 24 / 27 80.00% 8 / 10 24.79 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 traceEventReceived 100.00% 2 / 2 100.00% 1 / 1 2
 traceHandlerCalled 100.00% 2 / 2 100.00% 1 / 1 2
 traceSkippedWhilePaused 100.00% 3 / 3 100.00% 1 / 1 3
 traceScheduleRun 100.00% 2 / 2 100.00% 1 / 1 2
 traceScheduleRunSkippedWhilePaused 0.00% 0 / 2 0.00% 0 / 1 6
 describeSubscription 100.00% 4 / 4 100.00% 1 / 1 1
 describeEvent 83.33% 5 / 6 0.00% 0 / 1 7.23
 describeStateChange 100.00% 4 / 4 100.00% 1 / 1 3
 describeScheduledRun 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Worker;
6
7use Stewart\Contracts\Event\HaEvent;
8use Stewart\Contracts\Mqtt\MqttMessage;
9use Stewart\Contracts\Schedule\ScheduledRun;
10use Stewart\Contracts\State\StateChange;
11use Stewart\Contracts\Topic\TopicEvent;
12use Stewart\Contracts\Trigger\TriggerEvent;
13use Stewart\Runtime\Dispatch\RegisteredSubscription;
14use Stewart\Runtime\Logging\LogVerbosityPolicy;
15use Stewart\Runtime\Schedule\ScheduleOrigin;
16
17final readonly class AppTracer
18{
19    public function __construct(
20        private WorkerLogger $logger,
21        private LogVerbosityPolicy $verbosity,
22    ) {}
23
24    public function traceEventReceived(RegisteredSubscription $subscription, object $event): void
25    {
26        if ($this->verbosity->isTracing($subscription->scope)) {
27            $this->logger->forScope($subscription->scope)->recordTrace('Event received', [...$this->describeSubscription($subscription), ...$this->describeEvent($subscription, $event)]);
28        }
29    }
30
31    public function traceHandlerCalled(RegisteredSubscription $subscription): void
32    {
33        if ($this->verbosity->isTracing($subscription->scope)) {
34            $this->logger->forScope($subscription->scope)->recordTrace('Handler called', $this->describeSubscription($subscription));
35        }
36    }
37
38    public function traceSkippedWhilePaused(RegisteredSubscription $subscription, ?object $event): void
39    {
40        if ($this->verbosity->isTracing($subscription->scope)) {
41            $details = $event === null ? [] : $this->describeEvent($subscription, $event);
42            $this->logger->forScope($subscription->scope)->recordTrace('Event skipped while paused', [...$this->describeSubscription($subscription), ...$details]);
43        }
44    }
45
46    public function traceScheduleRun(ScheduleOrigin $origin, ScheduledRun $run): void
47    {
48        if ($this->verbosity->isTracing($origin->scope)) {
49            $this->logger->forScope($origin->scope)->recordTrace('Schedule ran', $this->describeScheduledRun($origin, $run));
50        }
51    }
52
53    public function traceScheduleRunSkippedWhilePaused(ScheduleOrigin $origin, ScheduledRun $run): void
54    {
55        if ($this->verbosity->isTracing($origin->scope)) {
56            $this->logger->forScope($origin->scope)->recordTrace('Schedule run skipped while paused', $this->describeScheduledRun($origin, $run));
57        }
58    }
59
60    /** @return array<string, string> */
61    private function describeSubscription(RegisteredSubscription $subscription): array
62    {
63        return [
64            'subscription' => $subscription->kind->value . ' ' . $subscription->describeMatch(),
65            'id' => $subscription->id->value,
66        ];
67    }
68
69    /** @return array<string, string|null> */
70    private function describeEvent(RegisteredSubscription $subscription, object $event): array
71    {
72        return match (true) {
73            $event instanceof StateChange => $this->describeStateChange($subscription, $event),
74            $event instanceof HaEvent => ['event' => $event->type],
75            $event instanceof TriggerEvent => ['platform' => $event->getPlatform()],
76            $event instanceof TopicEvent => ['topic' => $event->topic, 'from' => $event->publisher?->value],
77            $event instanceof MqttMessage => ['topic' => $event->topic],
78            default => [],
79        };
80    }
81
82    /** @return array<string, string> */
83    private function describeStateChange(RegisteredSubscription $subscription, StateChange $change): array
84    {
85        $details = ['change' => \sprintf('%s → %s', $change->from->state ?? 'none', $change->to->state ?? 'none')];
86
87        // An exact match already names the entity.
88        if ($subscription->entityFilter !== null || $subscription->selector->findExactPattern() === null) {
89            $details['entity'] = $change->entityId->value;
90        }
91
92        return $details;
93    }
94
95    /** @return array<string, string> */
96    private function describeScheduledRun(ScheduleOrigin $origin, ScheduledRun $run): array
97    {
98        return ['schedule' => $origin->label(), 'scheduled_for' => (string) $run->scheduledFor];
99    }
100}