Passed
Push — feature/add-config-files ( 1c4158...837ed6 )
by Jesús
05:48 queued 02:26
created

Config::loadAllConfigValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
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 40
    public static function getInstance(): self
33
    {
34 40
        if (self::$instance === null) {
35 8
            self::$instance = new self();
36
        }
37
38 40
        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 31
    public function get(string $key, $default = self::DEFAULT_CONFIG_VALUE)
57
    {
58 31
        if (empty($this->config)) {
59 15
            $this->init();
60
        }
61
62 31
        if ($default !== self::DEFAULT_CONFIG_VALUE && !$this->hasValue($key)) {
63 22
            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 40
    public function init(): void
79
    {
80 40
        $this->configFactory = null;
81 40
        $this->config = $this->loadAllConfigValues();
82
    }
83
84 40
    public function setAppRootDir(string $dir): self
85
    {
86 40
        $this->appRootDir = $dir;
87
88 40
        if (empty($this->appRootDir)) {
89
            $this->appRootDir = getcwd() ?: ''; // @codeCoverageIgnore
90
        }
91
92 40
        return $this;
93
    }
94
95 40
    public function getAppRootDir(): string
96
    {
97 40
        return $this->appRootDir ?? getcwd() ?: '';
98
    }
99
100 23
    public function getCacheDir(): string
101
    {
102 23
        return $this->getAppRootDir() . '/cache/';
103
    }
104
105 40
    public function setSetup(SetupGacelaInterface $setup): self
106
    {
107 40
        $this->setup = $setup;
108
109 40
        return $this;
110
    }
111
112
    /**
113
     * @internal
114
     */
115 40
    public function getFactory(): ConfigFactory
116
    {
117 40
        if ($this->configFactory === null) {
118 40
            $this->configFactory = new ConfigFactory(
119 40
                $this->getAppRootDir(),
120 40
                $this->setup ?? new SetupGacela()
121
            );
122
        }
123
124 40
        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 31
    private function hasValue(string $key): bool
128
    {
129 31
        return array_key_exists($key, $this->config);
130
    }
131
132
    /**
133
     * @return array<string,mixed>
134
     */
135 40
    private function loadAllConfigValues(): array
136
    {
137 40
        return $this->getFactory()
138 40
            ->createConfigLoader()
139 40
            ->loadAll();
140
    }
141
}
142