Passed
Push — master ( fa1416...da5f2f )
by Jesús
04:02 queued 12s
created

Config   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 88%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 21
eloc 40
c 3
b 0
f 0
dl 0
loc 146
ccs 44
cts 50
cp 0.88
rs 10

13 Methods

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