Completed
Push — master ( 1f1a8e...ab56b8 )
by Dmitry
03:54
created

Config::require()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Basis;
4
5
use ArrayAccess;
6
use Closure;
7
8
class Config implements ArrayAccess
9
{
10
    private $converter;
11
12 37
    public function __construct(Framework $framework, Filesystem $fs, Converter $converter)
13
    {
14 37
        $this->converter = $converter;
15
16 37
        $filename = $fs->getPath("config.php");
17 37
        if (!file_exists($filename)) {
18
            $filename = $framework->getPath('resources/default/config.php');
19
        }
20 37
        $data = include $filename;
21 37
        foreach ($data as $k => $v) {
22 37
            if ($v instanceof Closure) {
23 37
                $v = $v();
24
            }
25 37
            $this->$k = $v;
26 37
            if (is_array($v) || is_object($v)) {
27 37
                $this->$k = $converter->toObject($v);
28
            }
29
        }
30 37
    }
31
32 1
    public function offsetExists($offset)
33
    {
34 1
        return $this->getNode($offset) != null;
35
    }
36
37 37
    public function offsetGet($offset)
38
    {
39 37
        $value = $this->getNode($offset);
40 37
        if (!is_object($value)) {
41 37
            return $value;
42
        }
43
44 3
        return $this->converter->toArray($value);
45
    }
46
47 1
    public function offsetSet($offset, $value)
48
    {
49 1
        $path = explode('.', $offset);
50 1
        $key = array_pop($path);
51 1
        $parent = $this->getNode(implode('.', $path), true);
52 1
        if (is_array($value) || is_object($value)) {
53
            $value = $this->converter->toObject($value);
54
        }
55
56 1
        return $parent->$key = $value;
57
    }
58
59 1
    public function offsetUnset($offset)
60
    {
61 1
        $path = explode('.', $offset);
62 1
        $key = array_pop($path);
63 1
        $parent = $this->getNode(implode('.', $path));
64 1
        unset($parent->$key);
65 1
    }
66
67
    public function require(string $offset)
68
    {
69
        if (!isset($this[$offset])) {
70
            throw new Exception("No offset $offset");
71
        }
72
    }
73
74 37
    private function getNode(string $offset, bool $createIfNone = false)
75
    {
76 37
        if (!$offset) {
77 1
            return $this;
78
        }
79 37
        $current = $this;
80 37
        foreach (explode('.', $offset) as $key) {
81 37
            if (is_object($current)) {
82 37
                if (!property_exists($current, $key)) {
83 1
                    if ($createIfNone) {
84 1
                        $current->$key = (object) [];
85
                    } else {
86 1
                        return null;
87
                    }
88
                }
89 37
                $current = $current->$key;
90
            }
91
        }
92
93 37
        return $current;
94
    }
95
}
96