Completed
Push — master ( e7b929...ba2691 )
by Dmitry
03:11
created

Config   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 85
ccs 0
cts 70
cp 0
rs 10

7 Methods

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