Passed
Pull Request — master (#213)
by Ankit
01:59
created

Config::set()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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