Passed
Push — main ( eaed76...228f96 )
by Chema
10:35 queued 06:34
created

ConfigLoader::loadConfigsFromPatterns()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 19
ccs 9
cts 9
cp 1
crap 3
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Config;
6
7
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFileInterface;
8
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigItem;
9
10
final class ConfigLoader
11
{
12 111
    /** @var array<string,array<string,mixed>> */
13
    private array $cachedConfigs = [];
14
15
    public function __construct(
16
        private readonly GacelaConfigFileInterface $gacelaConfigFile,
17 111
        private readonly PathFinderInterface $pathFinder,
18
        private readonly PathNormalizerInterface $pathNormalizer,
19
    ) {
20
    }
21
22 111
    /**
23
     * @return array<string,mixed>
24 111
     */
25 111
    public function loadAll(): array
26
    {
27
        $allConfigs = [];
28 111
29 111
        foreach ($this->gacelaConfigFile->getConfigItems() as $configItem) {
30 32
            $allConfigs[] = $this->loadConfigsFromPatterns($configItem);
31 32
            $allConfigs[] = $this->loadLocalConfig($configItem);
32
        }
33
34 111
        return array_merge(...$allConfigs);
35 32
    }
36 32
37
    /**
38
     * @return array<string,mixed>
39
     */
40 111
    private function loadConfigsFromPatterns(GacelaConfigItem $configItem): array
41 111
    {
42
        $patterns = [
43 111
            $this->pathNormalizer->normalizePathPattern($configItem),
44
            $this->pathNormalizer->normalizePathPatternWithEnvironment($configItem),
45
        ];
46
47
        $localPath = $this->pathNormalizer->normalizePathLocal($configItem);
48
        $mergedConfigs = [];
49 111
50
        foreach ($patterns as $pattern) {
51 111
            $configPaths = $this->getMatchingConfigPaths($pattern, $localPath);
52 111
53
            foreach ($configPaths as $absolutePath) {
54 111
                $mergedConfigs[] = $this->readConfigWithCache($absolutePath, $configItem);
55 32
            }
56 32
        }
57
58
        return array_merge(...$mergedConfigs);
59 111
    }
60
61
    /**
62 32
     * @return array<string,mixed>
63
     */
64 32
    private function loadLocalConfig(GacelaConfigItem $configItem): array
65
    {
66
        $localPath = $this->pathNormalizer->normalizePathLocal($configItem);
67
        return $configItem->reader()->read($localPath);
68
    }
69
70
    /**
71
     * @return list<string>
72 32
     */
73
    private function getMatchingConfigPaths(string $pattern, string $excludePath): array
74
    {
75
        $matchingPaths = $this->pathFinder->matchingPattern($pattern);
76
        return array_diff($matchingPaths, [$excludePath]);
77 32
    }
78 32
79 32
    /**
80
     * @return array<string,mixed>
81
     */
82 32
    private function readConfigWithCache(string $absolutePath, GacelaConfigItem $configItem): array
83 32
    {
84 16
        if (!isset($this->cachedConfigs[$absolutePath])) {
85 16
            $this->cachedConfigs[$absolutePath] = $configItem->reader()->read($absolutePath);
86
        }
87
88 16
        return $this->cachedConfigs[$absolutePath];
89
    }
90
}
91