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

Config   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 56
ccs 0
cts 18
cp 0
rs 10
wmc 8

2 Methods

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