Completed
Push — master ( 011717...660375 )
by Dmitry
04:27
created

Config::__construct()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 16
Code Lines 10

Duplication

Lines 6
Ratio 37.5 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

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