Config::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
}