Passed
Pull Request — master (#179)
by Chema
06:30 queued 03:13
created

Config   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 97.73%

Importance

Changes 0
Metric Value
eloc 39
c 0
b 0
f 0
dl 0
loc 136
ccs 43
cts 44
cp 0.9773
rs 10
wmc 23

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setSetup() 0 5 1
A resetInstance() 0 3 1
A getFactory() 0 10 2
A __construct() 0 2 1
A getCacheDir() 0 3 1
A setAppRootDir() 0 9 3
A getAppRootDir() 0 3 2
A get() 0 15 5
A init() 0 4 1
A getInstance() 0 7 2
A getSetupGacela() 0 7 2
A hasValue() 0 3 1
A loadAllConfigValues() 0 5 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
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 44
    public static function getInstance(): self
33
    {
34 44
        if (self::$instance === null) {
35 8
            self::$instance = new self();
36
        }
37
38 44
        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 34
    public function get(string $key, $default = self::DEFAULT_CONFIG_VALUE)
57
    {
58 34
        if (empty($this->config)) {
59 16
            $this->init();
60
        }
61
62 34
        if ($default !== self::DEFAULT_CONFIG_VALUE && !$this->hasValue($key)) {
63 23
            return $default;
64
        }
65
66 19
        if (!$this->hasValue($key)) {
67 1
            throw ConfigException::keyNotFound($key, self::class);
68
        }
69
70 18
        return $this->config[$key];
71
    }
72
73
    /**
74
     * Force loading all config values in memory.
75
     *
76
     * @throws ConfigException
77
     */
78 44
    public function init(): void
79
    {
80 44
        $this->configFactory = null;
81 44
        $this->config = $this->loadAllConfigValues();
82
    }
83
84 44
    public function setAppRootDir(string $dir): self
85
    {
86 44
        $this->appRootDir = $dir;
87
88 44
        if (empty($this->appRootDir)) {
89
            $this->appRootDir = getcwd() ?: ''; // @codeCoverageIgnore
90
        }
91
92 44
        return $this;
93
    }
94
95 44
    public function getAppRootDir(): string
96
    {
97 44
        return $this->appRootDir ?? getcwd() ?: '';
98
    }
99
100 24
    public function getCacheDir(): string
101
    {
102 24
        return $this->getAppRootDir() . '/' . $this->getSetupGacela()->getCacheDirectory();
103
    }
104
105 44
    public function setSetup(SetupGacelaInterface $setup): self
106
    {
107 44
        $this->setup = $setup;
108
109 44
        return $this;
110
    }
111
112
    /**
113
     * @internal
114
     */
115 44
    public function getFactory(): ConfigFactory
116
    {
117 44
        if ($this->configFactory === null) {
118 44
            $this->configFactory = new ConfigFactory(
119 44
                $this->getAppRootDir(),
120 44
                $this->getSetupGacela()
121
            );
122
        }
123
124 44
        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 44
    public function getSetupGacela(): SetupGacelaInterface
128
    {
129 44
        if ($this->setup === null) {
130
            $this->setup = new SetupGacela();
131
        }
132
133 44
        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...
134
    }
135
136 34
    private function hasValue(string $key): bool
137
    {
138 34
        return array_key_exists($key, $this->config);
139
    }
140
141
    /**
142
     * @return array<string,mixed>
143
     */
144 44
    private function loadAllConfigValues(): array
145
    {
146 44
        return $this->getFactory()
147 44
            ->createConfigLoader()
148 44
            ->loadAll();
149
    }
150
}
151