Passed
Push — master ( 59d7dc...72f8f4 )
by Jesús
04:25 queued 12s
created

Config::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework;
6
7
use Gacela\Framework\Config\ConfigFactory;
8
use Gacela\Framework\Exception\ConfigException;
9
10
final class Config
11
{
12
    public const DEFAULT_CONFIG_VALUE = 'Gacela\Framework\Config::DEFAULT_CONFIG_VALUE';
13
14
    private static ?self $instance = null;
15
16
    private ?string $appRootDir = null;
17
18
    /** @var array<string,mixed> */
19
    private array $config = [];
20
21
    /** @var array<string,mixed> */
22
    private array $globalServices = [];
23
24
    private ?ConfigFactory $configFactory = null;
25
26 5
    private function __construct()
27
    {
28 5
    }
29
30 24
    public static function getInstance(): self
31
    {
32 24
        if (self::$instance === null) {
33 5
            self::$instance = new self();
34
        }
35
36 24
        return self::$instance;
37
    }
38
39
    /**
40
     * @internal for testing purposes
41
     */
42 4
    public static function resetInstance(): void
43
    {
44 4
        self::$instance = null;
45 4
    }
46
47
    /**
48
     * @param null|mixed $default
49
     *
50
     * @throws ConfigException
51
     *
52
     * @return mixed
53
     */
54 10
    public function get(string $key, $default = self::DEFAULT_CONFIG_VALUE)
55
    {
56 10
        if (empty($this->config)) {
57 4
            $this->init();
58
        }
59
60 10
        if ($default !== self::DEFAULT_CONFIG_VALUE && !$this->hasValue($key)) {
61 3
            return $default;
62
        }
63
64 8
        if (!$this->hasValue($key)) {
65 1
            throw ConfigException::keyNotFound($key, self::class);
66
        }
67
68 7
        return $this->config[$key];
69
    }
70
71
    /**
72
     * Force loading all config values in memory.
73
     *
74
     * @throws ConfigException
75
     */
76 24
    public function init(): void
77
    {
78 24
        $this->configFactory = null;
79 24
        $this->config = $this->loadAllConfigValues();
80 24
    }
81
82 20
    public function setAppRootDir(string $dir): self
83
    {
84 20
        $this->appRootDir = $dir;
85
86 20
        if (empty($this->appRootDir)) {
87
            $this->appRootDir = getcwd() ?: ''; // @codeCoverageIgnore
88
        }
89
90 20
        return $this;
91
    }
92
93 24
    public function getAppRootDir(): string
94
    {
95 24
        return $this->appRootDir ?? getcwd() ?: '';
96
    }
97
98
    /**
99
     * @param array<string,mixed> $globalServices
100
     */
101 21
    public function setGlobalServices(array $globalServices): self
102
    {
103 21
        $this->globalServices = $globalServices;
104
105 21
        return $this;
106
    }
107
108
    /**
109
     * @internal
110
     */
111 24
    public function getFactory(): ConfigFactory
112
    {
113 24
        if (null === $this->configFactory) {
114 24
            $this->configFactory = new ConfigFactory(
115 24
                $this->getAppRootDir(),
116 24
                $this->globalServices
117
            );
118
        }
119
120 24
        return $this->configFactory;
121
    }
122
123 10
    private function hasValue(string $key): bool
124
    {
125 10
        return isset($this->config[$key]);
126
    }
127
128
    /**
129
     * @return array<string,mixed>
130
     */
131 24
    private function loadAllConfigValues(): array
132
    {
133 24
        return $this->getFactory()
134 24
            ->createConfigLoader()
135 24
            ->loadAll();
136
    }
137
}
138