Config::get()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 13
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 2
crap 20
1
<?php
2
namespace Staticus\Config;
3
4
class Config implements ConfigInterface
5
{
6
    protected $config = [];
7
    public function __construct($config)
8
    {
9
        $this->config = $config;
10
    }
11
12
    public function all()
13
    {
14
15
        return $this->config;
16
    }
17
18
    public function get($route, $default = null)
19
    {
20
        $config = $this->config;
21
        $routes = explode('.', $route);
22
        $endPath = '';
23
        foreach ($routes as $item) {
24
            $endPath .= $item . '.';
25
            if (!array_key_exists($item, $config)) {
26
                if (null !== $default) {
27
28
                    return $default;
29
                }
30
                throw new \RuntimeException('Unknown config route and no default values: ' . $endPath);
31
            }
32
            $config = $config[$item];
33
        }
34
35
        return $config;
36
    }
37
38
    /**
39
     * @param $route
40
     * @return bool
41
     */
42
    public function has($route)
43
    {
44
        $config = $this->config;
45
        $routes = explode('.', $route);
46
        $endPath = '';
47
        foreach ($routes as $item) {
48
            $endPath .= $item . '.';
49
            if (!array_key_exists($item, $config)) {
50
51
                return false;
52
            }
53
            $config = $config[$item];
54
        }
55
56
        return true;
57
    }
58
}