Lines 91.66% 11 / 12
Methods 50.00% 1 / 2
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 fromNamespace 90.90% 10 / 11 0.00% 0 / 1 5.02
11final readonly class GeneratedRoots
12{
13    private const int MAJOR_WITH_SERVICES_ROOT = 1;
14
15    /**
16     * @param class-string $entitiesClass
17     * @param class-string $actionsClass
18     * @param class-string<Manifest>|null $manifestClass
19     */
20    private function __construct(
21        public string $entitiesClass,
22        public string $actionsClass,
23        public ?string $manifestClass,
24    ) {}
25
26    /** @throws AppException */
27    public static function fromNamespace(string $generatedNamespace): ?self
28    {
29        $namespace = trim($generatedNamespace, '\\');
30        $entities = $namespace . '\\Entities';
31        $actions = $namespace . '\\Actions';
32
33        // Only the roots are probed before the tree is known to be current: an older Manifest or entity handle
34        // would fail to load against today's contracts, and PHP cannot catch that.
35        if (!class_exists($actions)) {
36            if (class_exists($namespace . '\\Services')) {
37                throw AppException::createForReason(AppError::GeneratedCodeFromOlderMajor, namespace: $namespace, major: self::MAJOR_WITH_SERVICES_ROOT);
38            }
39
40            return null;
41        }
42
43        if (!class_exists($entities)) {
44            return null;
45        }
46
47        $manifest = $namespace . '\\Manifest';
48
49        return new self($entities, $actions, is_a($manifest, Manifest::class, true) ? $manifest : null);
50    }
51}