Passed
Push — master ( 2b04c5...3174fa )
by Jesús
01:25 queued 12s
created

GacelaJsonConfigFile::dependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Config\GacelaFileConfig;
6
7
use function is_array;
8
9
/**
10
 * @psalm-suppress DeprecatedClass
11
 *
12
 * @deprecated
13
 */
14
final class GacelaJsonConfigFile implements GacelaConfigFileInterface
15
{
16
    /** @var array<string,GacelaConfigItemInterface> */
17
    private array $configs;
18
19
    /** @var array<string,string|callable> */
20
    private array $dependencies;
21
22
    /**
23
     * @param array<string,GacelaConfigItemInterface> $configs
24
     * @param array<string,string|callable> $dependencies
25
     */
26 8
    private function __construct(
27
        array $configs,
28
        array $dependencies
29
    ) {
30 8
        $this->configs = $configs;
31 8
        $this->dependencies = $dependencies;
32 8
    }
33
34
    /**
35
     * @param array{
36
     *     config: array<array>|array{type:string,path:string,path_local:string},
37
     *     dependencies: array<string,string|callable>,
38
     * } $json
39
     */
40 5
    public static function fromArray(array $json): self
41
    {
42 5
        return new self(
43 5
            self::getConfigItems($json['config'] ?? []),
44 5
            $json['dependencies'] ?? [],
45
        );
46
    }
47
48
    /**
49
     * @param array<array>|array{type:string,path:string,path_local:string} $config
50
     *
51
     * @return array<string,GacelaConfigItemInterface>
52
     */
53 5
    private static function getConfigItems(array $config): array
54
    {
55 5
        $first = reset($config);
56
57 5
        if (!is_array($first)) {
58
            /** @psalm-suppress DeprecatedClass */
59 2
            $c = GacelaJsonConfigItem::fromArray($config);
60 2
            return [$c->type() => $c];
61
        }
62
63 3
        $result = [];
64
65
        /** @var array<array{type:string,path:string,path_local:string}> $config */
66 3
        foreach ($config as $configItem) {
67
            /** @psalm-suppress DeprecatedClass */
68 3
            $c = GacelaJsonConfigItem::fromArray($configItem);
69 3
            $result[$c->type()] = $c;
70
        }
71
72 3
        return $result;
73
    }
74
75 3
    public static function withDefaults(): self
76
    {
77
        /** @psalm-suppress DeprecatedClass */
78 3
        $configItem = GacelaJsonConfigItem::withDefaults();
79
80 3
        return new self(
81 3
            [$configItem->type() => $configItem],
82 3
            []
83
        );
84
    }
85
86
    /**
87
     * @return array<string,GacelaConfigItemInterface>
88
     */
89 7
    public function configs(): array
90
    {
91 7
        return $this->configs;
92
    }
93
94
    /**
95
     * @return array<string,string|callable>
96
     */
97
    public function dependencies(): array
98
    {
99
        return $this->dependencies;
100
    }
101
}
102