Config::set()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

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 6
cts 6
cp 1
crap 4
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 4
    public static function set($config = null, bool $force = false)
22
    {
23 4
        if ( ! $force && ! empty(self::$config)) {
24 4
            return;
25
        }
26
27 2
        if (\is_array($config)) {
28 1
            self::$config = $config;
29
        } else {
30 1
            self::$config = require $config ?? self::DEFAULT_CONFIG_PATH;
31
        }
32 2
    }
33
34
    /**
35
     * Get config.
36
     *
37
     * @param string|null $key Key to extract.
38
     *
39
     * @return mixed
40
     */
41 6
    public static function get(string $key = null)
42
    {
43 6
        self::set();
44
45 6
        if (empty($key)) {
46 3
            return self::$config;
47
        }
48
49 3
        $keys  = explode('.', $key);
50 3
        $value = self::$config;
51
52 3
        foreach ($keys as $k) {
53 3
            if ( ! isset($value[$k])) {
54 1
                return null;
55
            }
56
57 3
            $value = $value[$k];
58
        }
59
60 2
        return $value;
61
    }
62
}
63