Completed
Push — master ( aa1d4d...acb052 )
by Dmitry
10:44 queued 08:35
created

Config::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 6
Ratio 50 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 6
loc 12
ccs 7
cts 8
cp 0.875
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 2
crap 4.0312
1
<?php
2
3
namespace Basis;
4
5
use ArrayAccess;
6
7
class Config implements ArrayAccess
8
{
9
    private $converter;
10
11 11
    public function __construct(Filesystem $fs, Converter $converter)
12
    {
13 11
        $this->converter = $converter;
14
15 11
        $data = include $fs->getPath("config.php");
16 11 View Code Duplication
        foreach ($data as $k => $v) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17 11
            $this->$k = $v;
18 11
            if (is_array($v) || is_object($v)) {
19
                $this->$k = $converter->toObject($v);
20
            }
21
        }
22 11
    }
23
24
    public function offsetExists($offset)
25
    {
26
        return $this->getNode($offset) != null;
27
    }
28
29 11
    public function offsetGet($offset)
30
    {
31 11
        $value = $this->getNode($offset);
32 11
        if (!is_object($value)) {
33 11
            return $value;
34
        }
35
36
        return $this->converter->toArray($value);
37
    }
38
39
    public function offsetSet($offset, $value)
40
    {
41
        $path = explode('.', $offset);
42
        $key = array_pop($path);
43
        $parent = $this->getNode(implode('.', $path), true);
44
        if (is_array($value) || is_object($value)) {
45
            $value = $this->converter->toObject($value);
46
        }
47
48
        return $parent->$key = $value;
49
    }
50
51
    public function offsetUnset($offset)
52
    {
53
        $path = explode('.', $offset);
54
        $key = array_pop($path);
55
        $parent = $this->getNode(implode('.', $path));
56
        unset($parent->$key);
57
    }
58
59
    public function shouldHave($offset)
60
    {
61
        if (!isset($this[$offset])) {
62
            throw new Exception("No offset $offset");
63
        }
64
    }
65
66 11
    private function getNode($offset, $createIfNone = false)
67
    {
68 11
        if (!$offset) {
69
            return $this;
70
        }
71 11
        $current = $this;
72 11
        foreach (explode('.', $offset) as $key) {
73 11
            if (is_object($current)) {
74 11
                if (!property_exists($current, $key)) {
75
                    if ($createIfNone) {
76
                        $current->$key = (object) [];
77
                    } else {
78
                        return;
79
                    }
80
                }
81 11
                $current = $current->$key;
82
            }
83
        }
84
85 11
        return $current;
86
    }
87
}
88