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

Config::setConfig()   A

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 0
Metric Value
cc 4
eloc 6
nc 3
nop 2
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 4
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|array $config
17
     * @param bool         $force
18
     *
19
     * @return void
20
     */
21 3
    public static function setConfig($config = null, bool $force = false)
22
    {
23 3
        if ( ! $force && ! empty(self::$config)) {
24 3
            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 5
    public static function get(string $key = null)
42
    {
43 5
        self::setConfig();
44
45 5
        if (empty($key)) {
46 3
            return self::$config;
47
        }
48
49 2
        $keys  = explode('.', $key);
50 2
        $value = self::$config;
51
52 2
        foreach ($keys as $key) {
53 2
            if ( ! isset($value[$key])) {
54 1
                return null;
55
            }
56
57 2
            $value = $value[$key];
58
        }
59
60 1
        return $value;
61
    }
62
}
63