Completed
Push — master ( 9d8998...346ad6 )
by Dmitry
03:39
created

Config::offsetGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2.032
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 22
    public function __construct(Framework $framework, Filesystem $fs, Converter $converter)
13
    {
14 22
        $this->converter = $converter;
15
16 22
        $filename = $fs->getPath("config.php");
17 22
        if (!file_exists($filename)) {
18
            $filename = $framework->getPath('resources/default/config.php');
19
        }
20 22
        $data = include $filename;
21 22
        foreach ($data as $k => $v) {
22 22
            if ($v instanceof Closure) {
23 22
                $v = $v();
24
            }
25 22
            $this->$k = $v;
26 22
            if (is_array($v) || is_object($v)) {
27 22
                $this->$k = $converter->toObject($v);
28
            }
29
        }
30 22
    }
31
32 1
    public function offsetExists($offset)
33
    {
34 1
        return $this->getNode($offset) != null;
35
    }
36
37 22
    public function offsetGet($offset)
38
    {
39 22
        $value = $this->getNode($offset);
40 22
        if (!is_object($value)) {
41 22
            return $value;
42
        }
43
44
        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 22
    private function getNode(string $offset, bool $createIfNone = false)
75
    {
76 22
        if (!$offset) {
77 1
            return $this;
78
        }
79 22
        $current = $this;
80 22
        foreach (explode('.', $offset) as $key) {
81 22
            if (is_object($current)) {
82 22
                if (!property_exists($current, $key)) {
83 1
                    if ($createIfNone) {
84 1
                        $current->$key = (object) [];
85
                    } else {
86 1
                        return null;
87
                    }
88
                }
89 22
                $current = $current->$key;
90
            }
91
        }
92
93 22
        return $current;
94
    }
95
}
96