Completed
Push — master ( 4848aa...64d9da )
by Dmitry
03:44
created

Config::__construct()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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