Completed
Pull Request — master (#71)
by Ankit
03:51
created

Config   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 49
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfig() 0 4 2
A get() 0 20 4
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