Completed
Push — master ( 1d9915...f239fd )
by Dmitry
04:37
created

Config::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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