Passed
Pull Request — master (#55)
by Jesús
03:20
created

GacelaConfigFile::getMappingInterfaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Config\GacelaFileConfig;
6
7
final class GacelaConfigFile
8
{
9
    /** @var array<string,GacelaConfigItem> */
10
    private array $configs;
11
12
    /** @var array<string,string|callable> */
13
    private array $mappingInterfaces;
14
15
    /**
16
     * @param array<string,GacelaConfigItem> $configs
17
     * @param array<string,string|callable>  $mappingInterfaces
18
     */
19 26
    private function __construct(
20
        array $configs,
21
        array $mappingInterfaces
22
    ) {
23 26
        $this->configs = $configs;
24 26
        $this->mappingInterfaces = $mappingInterfaces;
25 26
    }
26
27
    /**
28
     * @param array{
29
     *     config: array<array>|array{type:string,path:string,path_local:string},
30
     *     mapping-interfaces: array<string,string|callable>,
31
     * } $array
32
     */
33 11
    public static function fromArray(array $array): self
34
    {
35 11
        return new self(
36 11
            self::getConfigItems($array['config'] ?? []),
37 11
            $array['mapping-interfaces'] ?? []
38
        );
39
    }
40
41
    /**
42
     * @param array<array>|array{type:string,path:string,path_local:string} $config
43
     *
44
     * @return array<string,GacelaConfigItem>
45
     */
46 11
    private static function getConfigItems(array $config): array
47
    {
48 11
        if (self::isSingleConfigFile($config)) {
49 2
            $c = GacelaConfigItem::fromArray($config);
50 2
            return [$c->type() => $c];
51
        }
52
53 9
        $result = [];
54
55
        /** @var array<array{type:string,path:string,path_local:string}> $config */
56 9
        foreach ($config as $configItem) {
57 3
            $c = GacelaConfigItem::fromArray($configItem);
58 3
            $result[$c->type()] = $c;
59
        }
60
61 9
        return $result;
62
    }
63
64 11
    private static function isSingleConfigFile(array $config): bool
65
    {
66 11
        return isset($config['type'])
67 9
            || isset($config['path'])
68 11
            || isset($config['path_local']);
69
    }
70
71 15
    public static function withDefaults(): self
72
    {
73 15
        $configItem = GacelaConfigItem::withDefaults();
74
75 15
        return new self(
76 15
            [$configItem->type() => $configItem],
77 15
            []
78
        );
79
    }
80
81
    /**
82
     * @return array<string,GacelaConfigItem>
83
     */
84 26
    public function getConfigs(): array
85
    {
86 26
        return $this->configs;
87
    }
88
89
    /**
90
     * Map interfaces to concrete classes or callable (which will be resolved on runtime).
91
     * This is util to inject dependencies to Gacela services (such as Factories, for example) via their constructor.
92
     *
93
     * @return mixed
94
     */
95 1
    public function getMappingInterface(string $key)
96
    {
97 1
        return $this->mappingInterfaces[$key] ?? null;
98
    }
99
}
100