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

Config   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 54
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfig() 0 10 5
A get() 0 18 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|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 1
            return;
25
        }
26
27 2
        if (is_array($config)) {
28 1
            self::$config = $config;
29 1
        } elseif (is_string($config)) {
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
        if (empty($key)) {
44 3
            return self::$config;
45
        }
46
47 2
        $keys  = explode('.', $key);
48 2
        $value = self::$config;
49
50 2
        foreach ($keys as $key) {
51 2
            if ( ! isset($value[$key])) {
52 1
                return null;
53
            }
54
55 2
            $value = $value[$key];
56
        }
57
58 1
        return $value;
59
    }
60
}
61