Lines 100.00% 10 / 10
Functions and Methods 100.00% 5 / 5
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ManifestCheck 100.00% 10 / 10 100.00% 5 / 5 9 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 forGeneratedCode 100.00% 2 / 2 100.00% 1 / 1 2
 readFormatVersion 100.00% 3 / 3 100.00% 1 / 1 3
 hasCurrentFormat 100.00% 1 / 1 100.00% 1 / 1 1
 findDrift 100.00% 3 / 3 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Broker;
6
7use Stewart\Contracts\Generated\Manifest;
8use Stewart\Runtime\App\GeneratedRoots;
9use Stewart\Runtime\Config\CodegenConfig;
10use Stewart\Support\Wire\GeneratedFormat;
11
12final readonly class ManifestCheck
13{
14    /** @param class-string<Manifest> $manifest */
15    public function __construct(private string $manifest) {}
16
17    public static function forGeneratedCode(CodegenConfig $codegen): ?self
18    {
19        $manifest = GeneratedRoots::fromNamespace($codegen->namespace->value)?->manifestClass;
20
21        return $manifest === null ? null : new self($manifest);
22    }
23
24    public function readFormatVersion(): int
25    {
26        $constant = $this->manifest . '::' . GeneratedFormat::MANIFEST_CONSTANT;
27        $version = \defined($constant) ? \constant($constant) : 0;
28
29        return \is_int($version) ? $version : 0;
30    }
31
32    public function hasCurrentFormat(): bool
33    {
34        return $this->readFormatVersion() === GeneratedFormat::VERSION;
35    }
36
37    /** @param list<string> $liveEntityIds */
38    public function findDrift(array $liveEntityIds): ?ManifestDrift
39    {
40        $manifest = $this->manifest;
41        $drift = ManifestDrift::calculateBetween($manifest::listEntityIds(), $liveEntityIds, $manifest::listIgnoredEntityIds());
42
43        return $drift->isEmpty() ? null : $drift;
44    }
45}