Passed
Pull Request — master (#208)
by Jesús
07:17 queued 03:55
created

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