Passed
Push — master ( 889c24...e8b07e )
by Jesús
01:15 queued 11s
created

ConfigInit::readConfigItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ConfigInit::generateAbsolutePath() 0 6 1
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 12
    public function __construct(
22
        string $applicationRootDir,
23
        GacelaJsonConfigFactoryInterface $configFactory,
24
        PathFinderInterface $pathFinder,
25
        array $readers
26
    ) {
27 12
        $this->applicationRootDir = $applicationRootDir;
28 12
        $this->configFactory = $configFactory;
29 12
        $this->pathFinder = $pathFinder;
30 12
        $this->readers = $readers;
31 12
    }
32
33 12
    public function readAll(): array
34
    {
35 12
        $gacelaJsonConfig = $this->configFactory->createGacelaJsonConfig();
36 12
        $configs = [];
37
38 12
        foreach ($this->scanAllConfigFiles($gacelaJsonConfig) as $absolutePath) {
39 6
            $configs[] = $this->readConfigFromFile($gacelaJsonConfig, $absolutePath);
40
        }
41
42 12
        $configs[] = $this->readLocalConfigFile($gacelaJsonConfig);
43
44 12
        return array_merge(...$configs);
45
    }
46
47
    /**
48
     * @return string[]
49
     */
50 12
    private function scanAllConfigFiles(GacelaJsonConfig $gacelaJsonConfig): array
51
    {
52 12
        $configGroup = array_map(
53 12
            fn (GacelaJsonConfigItem $config): array => array_map(
54 12
                static fn ($p): string => (string)$p,
55 12
                array_diff(
56 12
                    $this->pathFinder->matchingPattern($this->generateAbsolutePath($config->path())),
57 12
                    [$this->generateAbsolutePath($config->pathLocal())]
58
                )
59 12
            ),
60 12
            $gacelaJsonConfig->configs()
61
        );
62
63 12
        return array_merge(...array_values($configGroup));
64
    }
65
66 6
    private function readConfigFromFile(GacelaJsonConfig $gacelaJson, string $absolutePath): array
67
    {
68 6
        $result = [];
69 6
        $configs = $gacelaJson->configs();
70
71 6
        foreach ($this->readers as $type => $reader) {
72 5
            $config = $configs[$type] ?? null;
73 5
            if ($config === null) {
74 4
                continue;
75
            }
76
77 4
            $result[] = $reader->canRead($absolutePath)
78 4
                ? $reader->read($absolutePath)
79 1
                : [];
80
        }
81
82 6
        return array_merge(...array_filter($result));
83
    }
84
85 12
    private function readLocalConfigFile(GacelaJsonConfig $gacelaJson): array
86
    {
87 12
        $result = [];
88 12
        $configs = $gacelaJson->configs();
89
90 12
        foreach ($this->readers as $type => $reader) {
91 11
            $config = $configs[$type] ?? null;
92 11
            if ($config === null) {
93 6
                continue;
94
            }
95 10
            $absolutePath = $this->generateAbsolutePath($config->pathLocal());
96
97 10
            $result[] = $reader->canRead($absolutePath)
98 9
                ? $reader->read($absolutePath)
99 2
                : [];
100
        }
101
102 12
        return array_merge(...array_filter($result));
103
    }
104
105 12
    private function generateAbsolutePath(string $relativePath): string
106
    {
107 12
        return sprintf(
108 12
            '%s/%s',
109 12
            $this->applicationRootDir,
110
            $relativePath
111
        );
112
    }
113
}
114