Passed
Push — feature/custom-caching-dir ( f598fb )
by Chema
04:58
created

Config::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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