Config   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 56
ccs 18
cts 18
cp 1
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 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