Completed
Push — master ( 42afef...5f18df )
by Dmitry
06:49 queued 03:50
created

Config::getNode()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.5625

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
ccs 12
cts 16
cp 0.75
rs 8.7624
cc 6
eloc 13
nc 6
nop 2
crap 6.5625
1
<?php
2
3
namespace Basis;
4
5
use ArrayAccess;
6
use Dotenv\Dotenv;
7
8
class Config implements ArrayAccess
9
{
10
    private $converter;
11
12 7
    function __construct(Filesystem $fs, Converter $converter)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
13
    {
14 7
        $this->converter = $converter;
15
16 7
        if($fs->exists('.env')) {
17
            $dotenv = new Dotenv($fs->path());
0 ignored issues
show
Bug introduced by
The method path() does not seem to exist on object<Basis\Filesystem>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
18
            $dotenv->load();
19
        }
20
21 7
        foreach ($fs->listFiles('resources/config') as $file) {
22 7
            $values = $this->converter->toObject(include $fs->getPath("resources/config/$file"));
23 7
            $current = $this;
24 7
            $name = substr($file, 0, -4);
25 7
            if (stristr($name, '/')) {
26
                $namespace = explode('/', $name);
27
                $name = array_pop($namespace);
28
                foreach ($namespace as $key) {
29
                    $current->$key = (object) [];
30
                    $current = $current->$key;
31
                }
32
            }
33 7
            $current->$name = $values;
34 7
        }
35 7
    }
36
37
    function offsetExists($offset)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
38
    {
39
        return $this->getNode($offset) != null;
40
    }
41
42 7
    function offsetGet($offset)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
43
    {
44 7
        $value = $this->getNode($offset);
45 7
        if(!is_object($value)) {
46 7
            return $value;
47
        }
48
49 1
        return $this->converter->toArray($value);
50
    }
51
52 1
    function offsetSet($offset, $value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
53
    {
54 1
        $path = explode('.', $offset);
55 1
        $key = array_pop($path);
56 1
        $parent = $this->getNode(implode('.', $path), true);
57 1
        if (is_array($value) || is_object($value)) {
58
            $value = $this->converter->toObject($value);
59
        }
60
61 1
        return $parent->$key = $value;
62
    }
63
64
    function offsetUnset($offset)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
65
    {
66
        $path = explode('.', $offset);
67
        $key = array_pop($path);
68
        $parent = $this->getNode(implode('.', $path));
69
        unset($parent->$key);
70
    }
71
72
    function shouldHave($offset)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
73
    {
74
        if(!isset($this[$offset])) {
75
            throw new Exception("No offset $offset");
76
        }
77
    }
78
79 7
    private function getNode($offset, $createIfNone = false)
80
    {
81 7
        if (!$offset) {
82
            return $this;
83
        }
84 7
        $current = $this;
85 7
        foreach (explode('.', $offset) as $key) {
86 7
            if (is_object($current)) {
87 7
                if (!property_exists($current, $key)) {
88 1
                    if ($createIfNone) {
89
                        $current->$key = (object) [];
90
                    } else {
91 1
                        return;
92
                    }
93
                }
94 7
                $current = $current->$key;
95 7
            }
96 7
        }
97
98 7
        return $current;
99
    }
100
}