Lines 90.00% 18 / 20
Methods 87.50% 7 / 8
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 mergeConfigTree 100.00% 9 / 9 100.00% 1 / 1 3
 loadConfig 100.00% 1 / 1 100.00% 1 / 1 1
 buildValidatedConfigTree 100.00% 3 / 3 100.00% 1 / 1 1
 locateConfigFile 100.00% 1 / 1 100.00% 1 / 1 1
 fingerprintConfigFile 0.00% 0 / 2 0.00% 0 / 1 6
 findMissingDefaultFile 100.00% 2 / 2 100.00% 1 / 1 3
 readConfigFile 100.00% 1 / 1 100.00% 1 / 1 2
13final readonly class ConfigLoader
14{
15    private const string MISSING_FILE_FINGERPRINT = 'missing';
16
17    private const string DEFAULT_CONFIG_FILE = 'stewart.yaml';
18
19    public function __construct(
20        private EnvironmentPlaceholders $placeholders,
21        private EnvironmentOverlay $overlay,
22        private ConfigFileReader $files,
23        private ProjectRoot $projectRoot,
24        private StewartConfigSchema $configuration,
25        private RenamedSettings $renamedSettings,
26        private GeneratedNamespaceResolver $generatedNamespaces,
27    ) {}
28
29    /**
30     * @param array<string, mixed> $cliOverrides
31     * @return array<array-key, mixed>
32     * @throws ConfigurationException
33     */
34    public function mergeConfigTree(?string $path, array $cliOverrides = []): array
35    {
36        $tree = $this->configuration->buildConfigTree();
37        $fileConfig = $this->placeholders->resolve($tree, $this->readConfigFile($path));
38        $this->renamedSettings->refuseRenamedKeys($fileConfig);
39        $configs = [$fileConfig, $this->overlay->collectEnvironmentOverrides($tree, $fileConfig)];
40
41        if ($cliOverrides !== []) {
42            $configs[] = $cliOverrides;
43        }
44
45        try {
46            return new Processor()->process($tree, $configs);
47        } catch (InvalidConfigurationException $e) {
48            throw ConfigurationException::createForReason(ConfigurationError::SchemaViolation, $e);
49        }
50    }
51
52    /**
53     * @param array<string, mixed> $cliOverrides
54     * @throws ConfigurationException
55     * @throws IdentifierException
56     */
57    public function loadConfig(?string $path, array $cliOverrides = []): StewartConfig
58    {
59        return StewartConfig::fromSection(ConfigSection::forRoot($this->mergeConfigTree($path, $cliOverrides)), $this->generatedNamespaces);
60    }
61
62    /**
63     * @return array<array-key, mixed>
64     * @throws ConfigurationException
65     * @throws IdentifierException
66     */
67    public function buildValidatedConfigTree(?string $path): array
68    {
69        $tree = $this->mergeConfigTree($path);
70        StewartConfig::fromSection(ConfigSection::forRoot($tree), $this->generatedNamespaces);
71
72        return $tree;
73    }
74
75    public function locateConfigFile(?string $path): string
76    {
77        return $path ?? $this->projectRoot->resolvePath(self::DEFAULT_CONFIG_FILE);
78    }
79
80    public function fingerprintConfigFile(?string $path): string
81    {
82        $file = $this->locateConfigFile($path);
83
84        return is_file($file) ? (string) hash_file('xxh128', $file) : self::MISSING_FILE_FINGERPRINT;
85    }
86
87    public function findMissingDefaultFile(?string $path): ?string
88    {
89        $defaultFile = $this->locateConfigFile(null);
90
91        return $path === null && !is_file($defaultFile) ? $defaultFile : null;
92    }
93
94    /**
95     * @return array<array-key, mixed>
96     * @throws ConfigurationException
97     */
98    private function readConfigFile(?string $path): array
99    {
100        // Container setups may configure everything through the environment.
101        return $this->findMissingDefaultFile($path) !== null ? [] : $this->files->readYamlMap($this->locateConfigFile($path));
102    }
103}