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

Config   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 3
dl 0
loc 88
ccs 42
cts 49
cp 0.8571
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 19 6
A offsetExists() 0 4 1
A offsetSet() 0 11 3
A offsetUnset() 0 7 1
A require() 0 6 2
B getNode() 0 21 6
A offsetGet() 0 9 2
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