Completed
Push — master ( 7c2ac0...7f1c12 )
by Dmitry
08:32
created

Config::offsetSet()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 3
eloc 7
nc 2
nop 2
crap 3.0261
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
    public function __construct(Filesystem $fs, Converter $converter)
13
    {
14 7
        $this->converter = $converter;
15
16 7
        if($fs->exists('.env')) {
17
            $dotenv = new Dotenv($fs->getPath());
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
        }
35 7
    }
36
37
    public function offsetExists($offset)
38
    {
39
        return $this->getNode($offset) != null;
40
    }
41
42 7
    public function offsetGet($offset)
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
    public function offsetSet($offset, $value)
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
    public function offsetUnset($offset)
65
    {
66
        $path = explode('.', $offset);
67
        $key = array_pop($path);
68
        $parent = $this->getNode(implode('.', $path));
69
        unset($parent->$key);
70
    }
71
72
    public function shouldHave($offset)
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
            }
96
        }
97
98 7
        return $current;
99
    }
100
}