Lines 100.00% 21 / 21
Methods 100.00% 3 / 3
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 replaceGeneratedManifest 100.00% 1 / 1 100.00% 1 / 1 1
 recheckGeneratedCode 100.00% 19 / 19 100.00% 1 / 1 9
12final class GeneratedCodeDrift
13{
14    public private(set) ?GeneratedCodeStatus $status = null;
15
16    public function __construct(
17        private readonly HaSession $session,
18        private readonly Clock $clock,
19        private readonly LoggerInterface $logger,
20        private ?GeneratedManifest $manifest = null,
21    ) {}
22
23    public function replaceGeneratedManifest(?GeneratedManifest $manifest): void
24    {
25        $this->manifest = $manifest;
26    }
27
28    // Rechecked often, so a warning repeats only when what it says has changed.
29    public function recheckGeneratedCode(): void
30    {
31        if ($this->manifest === null) {
32            $this->status = null;
33
34            return;
35        }
36
37        $previous = $this->status;
38        $drift = $this->manifest->findDrift($this->session->listEntityIds());
39        $status = new GeneratedCodeStatus($this->manifest->formatVersion, GeneratedFormat::VERSION, $drift->added, $drift->removed, $this->clock->getNow());
40        $this->status = $status;
41
42        if (!$status->hasCurrentFormat() && $previous?->format !== $status->format) {
43            $this->logger->warning('Generated classes were written by another stewart generate format; run stewart generate', [
44                'format' => $status->format,
45                'expected_format' => $status->expectedFormat,
46            ]);
47        }
48
49        if ($status->hasDrift() && ($previous === null || !$previous->hasSameDriftAs($status))) {
50            $this->logger->warning('Generated entity classes no longer match Home Assistant; run stewart generate', [
51                'added' => $status->added,
52                'removed' => $status->removed,
53            ]);
54        } elseif (!$status->hasDrift() && $previous?->hasDrift() === true) {
55            $this->logger->info('Generated entity classes match Home Assistant again');
56        }
57    }
58}