Passed
Push — master ( fa1416...da5f2f )
by Jesús
04:02 queued 12s
created

ConfigLoader::readLocalConfigFile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 18
ccs 11
cts 12
cp 0.9167
rs 9.9332
cc 4
nc 4
nop 1
crap 4.0092
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Config;
6
7
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFile;
8
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigItem;
9
10
final class ConfigLoader
11
{
12
    private string $applicationRootDir;
13
14
    private GacelaConfigFileFactoryInterface $configFactory;
15
16
    private PathFinderInterface $pathFinder;
17
18 27
    public function __construct(
19
        string $applicationRootDir,
20
        GacelaConfigFileFactoryInterface $configFactory,
21
        PathFinderInterface $pathFinder
22
    ) {
23 27
        $this->applicationRootDir = $applicationRootDir;
24 27
        $this->configFactory = $configFactory;
25 27
        $this->pathFinder = $pathFinder;
26 27
    }
27
28
    /**
29
     * @return array<string,mixed>
30
     */
31 27
    public function loadAll(): array
32
    {
33 27
        $gacelaFileConfig = $this->configFactory->createGacelaFileConfig();
34 27
        $configs = [];
35
36 27
        foreach ($this->scanAllConfigFiles($gacelaFileConfig) as $absolutePath) {
37 6
            $configs[] = $this->readConfigFromFile($gacelaFileConfig, $absolutePath);
38
        }
39
40 27
        $configs[] = $this->readLocalConfigFile($gacelaFileConfig);
41
42 27
        return array_merge(...$configs);
43
    }
44
45
    /**
46
     * All config files except the local config file.
47
     *
48
     * @return iterable<string>
49
     */
50 27
    private function scanAllConfigFiles(GacelaConfigFile $gacelaFileConfig): iterable
51
    {
52 27
        $configGroup = array_map(
53 27
            fn (GacelaConfigItem $config): array => array_diff(
54 27
                $this->pathFinder->matchingPattern($this->generateAbsolutePath($config->path())),
55 27
                [$this->generateAbsolutePath($config->pathLocal())]
56 27
            ),
57 27
            $gacelaFileConfig->getConfigItems()
58
        );
59
60 27
        $groupsValues = array_values($configGroup);
61
62 27
        foreach (array_merge(...$groupsValues) as $path) {
63 6
            yield $path;
64
        }
65 27
    }
66
67
    /**
68
     * @return array<string,mixed>
69
     */
70 6
    private function readConfigFromFile(GacelaConfigFile $gacelaConfigFile, string $absolutePath): array
71
    {
72 6
        $result = [];
73 6
        $configItems = $gacelaConfigFile->getConfigItems();
74
75 6
        foreach ($gacelaConfigFile->getConfigReaders() as $type => $reader) {
76 6
            $config = $configItems[$type] ?? null;
77 6
            if ($config === null) {
78
                continue;
79
            }
80
81 6
            $result[] = $reader->canRead($absolutePath)
82 4
                ? $reader->read($absolutePath)
83 3
                : [];
84
        }
85
86 6
        return array_merge(...array_filter($result));
87
    }
88
89
    /**
90
     * @return array<string,mixed>
91
     */
92 27
    private function readLocalConfigFile(GacelaConfigFile $gacelaConfigFile): array
93
    {
94 27
        $result = [];
95 27
        $configItems = $gacelaConfigFile->getConfigItems();
96
97 27
        foreach ($gacelaConfigFile->getConfigReaders() as $type => $reader) {
98 27
            $config = $configItems[$type] ?? null;
99 27
            if ($config === null) {
100
                continue;
101
            }
102 27
            $absolutePath = $this->generateAbsolutePath($config->pathLocal());
103
104 27
            $result[] = $reader->canRead($absolutePath)
105 27
                ? $reader->read($absolutePath)
106 1
                : [];
107
        }
108
109 27
        return array_merge(...array_filter($result));
110
    }
111
112 27
    private function generateAbsolutePath(string $relativePath): string
113
    {
114 27
        return sprintf(
115 27
            '%s/%s',
116 27
            $this->applicationRootDir,
117
            $relativePath
118
        );
119
    }
120
}
121