Completed
Pull Request — master (#71)
by Ankit
02:04
created

Config::setConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TusPhp;
4
5
class Config
6
{
7
    /** @const string */
8
    const DEFAULT_CONFIG_PATH = __DIR__ . '/config/default.php';
9
10
    /** @var array */
11
    protected static $config = [];
12
13
    /**
14
     * Load default application configs.
15
     *
16
     * @param string|null $path
17
     *
18
     * @return void
19
     */
20
    public static function setConfig($path = null)
21
    {
22
        if (empty(self::$config)) {
23
            self::$config = require $path ?? self::DEFAULT_CONFIG_PATH;
24
        }
25
    }
26
27
    /**
28
     * Get config.
29
     *
30
     * @param string|null $key  Key to extract.
31
     *
32
     * @return mixed
33
     */
34
    public static function get(string $key = null)
35
    {
36
        self::setConfig();
37
38
        if (empty($key)) {
39
            return self::$config;
40
        }
41
42
        $keys  = explode('.', $key);
43
        $value = self::$config;
44
45
        foreach ($keys as $key) {
46
            if ( ! isset($value[$key])) {
47
                return null;
48
            }
49
50
            $value = $value[$key];
51
        }
52
53
        return $value;
54
    }
55
}
56