Passed
Push — master ( 73c257...7773a0 )
by Chema
01:11 queued 12s
created

ConfigInit::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Config;
6
7
final class ConfigInit
8
{
9
    private string $applicationRootDir;
10
11
    private GacelaJsonConfigFactoryInterface $configFactory;
12
13
    private PathFinderInterface $pathFinder;
14
15
    /** @var array<string,ConfigReaderInterface> */
16
    private array $readers;
17
18
    /**
19
     * @param array<string,ConfigReaderInterface> $readers
20
     */
21 8
    public function __construct(
22
        string $applicationRootDir,
23
        GacelaJsonConfigFactoryInterface $configFactory,
24
        PathFinderInterface $pathFinder,
25
        array $readers
26
    ) {
27 8
        $this->applicationRootDir = $applicationRootDir;
28 8
        $this->configFactory = $configFactory;
29 8
        $this->pathFinder = $pathFinder;
30 8
        $this->readers = $readers;
31 8
    }
32
33 8
    public function readAll(): array
34
    {
35 8
        $gacelaJsonConfig = $this->configFactory->createGacelaJsonConfig();
36 8
        $configs = [];
37
38 8
        foreach ($this->scanAllConfigFiles($gacelaJsonConfig) as $absolutePath) {
39 5
            $configs[] = $this->readConfigFromFile($gacelaJsonConfig, $absolutePath);
40
        }
41
42 7
        $configs[] = $this->readLocalConfigFile($gacelaJsonConfig);
43
44 7
        return array_merge(...$configs);
45
    }
46
47
    /**
48
     * @return string[]
49
     */
50 8
    private function scanAllConfigFiles(GacelaJsonConfig $gacelaJsonConfig): array
51
    {
52 8
        $configGroup = array_map(
53 8
            fn (GacelaJsonConfigItem $config): array => array_map(
54 8
                static fn ($p): string => (string)$p,
55 8
                array_diff(
56 8
                    $this->pathFinder->matchingPattern($this->generateAbsolutePath($config->path())),
57 8
                    [$this->generateAbsolutePath($config->pathLocal())]
58
                )
59 8
            ),
60 8
            $gacelaJsonConfig->configs()
61
        );
62
63 8
        return array_merge(...$configGroup);
64
    }
65
66 5
    private function readConfigFromFile(GacelaJsonConfig $gacelaJson, string $absolutePath): array
67
    {
68 5
        $result = [];
69
70 5
        foreach ($gacelaJson->configs() as $config) {
71 5
            $result[] = $this->readConfigItem($config, $absolutePath);
72
        }
73
74 4
        return array_merge(...array_filter($result));
75
    }
76
77 7
    private function readLocalConfigFile(GacelaJsonConfig $gacelaJson): array
78
    {
79 7
        $result = [];
80
81 7
        foreach ($gacelaJson->configs() as $config) {
82 7
            $absolutePath = $this->generateAbsolutePath($config->pathLocal());
83
84 7
            $result[] = $this->readConfigItem($config, $absolutePath);
85
        }
86
87 7
        return array_merge(...array_filter($result));
88
    }
89
90 8
    private function generateAbsolutePath(string $relativePath): string
91
    {
92 8
        return sprintf(
93 8
            '%s/%s',
94 8
            $this->applicationRootDir,
95
            $relativePath
96
        );
97
    }
98
99 8
    private function readConfigItem(GacelaJsonConfigItem $config, string $absolutePath): array
100
    {
101 8
        $reader = $this->readers[$config->type()] ?? null;
102
103 8
        if ($reader === null) {
104 1
            throw ConfigReaderException::notSupported($config->type(), $this->readers);
105
        }
106
107 7
        if ($reader->canRead($absolutePath)) {
108 6
            return $reader->read($absolutePath);
109
        }
110
111 2
        return [];
112
    }
113
}
114