Completed
Push — master ( 567b7d...5b8acd )
by James Ekow Abaka
01:48
created

Config   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.56%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 0
loc 72
ccs 43
cts 45
cp 0.9556
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isKeySet() 0 3 1
A __construct() 0 13 4
A readPath() 0 3 1
A get() 0 3 2
A setContext() 0 3 1
A set() 0 8 2
A setValue() 0 9 3
B expand() 0 15 6
1
<?php
2
3
namespace ntentan\config;
4
5
class Config 
6
{
7
8
    private $config;
9
    private $context = 'default';
10
11 11
    public function __construct($path = null, $namespace = null) {
12 11
        if (is_dir($path)) {
13 7
            $dir = new Directory($path);
14 7
            $this->config = ['default' => $this->expand($dir->parse(), $namespace)];
15 7
            foreach ($dir->getContexts() as $context) {
16 3
                $this->config[$context] = array_merge(
17 7
                        $this->config['default'], $this->expand($dir->parse($context), $namespace)
18
                );
19
            }
20 4
        } else if (is_file($path)) {
21 2
            $this->config[$this->context] = $this->expand(File::read($path), $namespace);
22
        }
23 11
    }
24
    
25 9
    public static function readPath($path, $namespace = null) {
26 9
        return new Config($path, $namespace);
27
    }
28
29
    public function isKeySet($key) {
30
        return isset($this->config[$this->context][$key]);
31
    }
32
33 11
    public function get($key, $default = null) {
34 11
        return isset($this->config[$this->context][$key]) ? $this->config[$this->context][$key] : $default;
35
    }
36
37 2
    public function setContext($context) {
38 2
        $this->context = $context;
39 2
    }
40
41 5
    public function set($key, $value) {
42 5
        $keys = explode('.', $key);
43 5
        $this->config[$this->context] = $this->setValue($keys, $value, $this->config[$this->context]);
44 5
        $this->config[$this->context][$key] = $value;
45 5
        if (is_array($value)) {
46 2
            $this->config[$this->context] += $this->expand($value, null, $key);
47
        }
48 5
    }
49
50 5
    private function setValue($keys, $value, $config) {
51 5
        if (!empty($keys)) {
52 5
            $key = array_shift($keys);
53 5
            $config[$key] = $this->setValue($keys, $value, isset($config[$key]) ? $config[$key] : []);
54 5
            return $config;
55
        } else {
56 5
            return $value;
57
        }
58
    }
59
60 10
    private function expand($array, $namespace, $prefix = null) {
61 10
        $config = [];
62 10
        if (!is_array($array))
63 10
            return $config;
64 10
        $dottedNamespace = $namespace ? "$namespace:" : "";
65 10
        $dottedPrefix = $prefix ? "$prefix." : "";
66 10
        foreach ($array as $key => $value) {
67 10
            $newPrefix = $dottedNamespace . $dottedPrefix . $key;
68 10
            $config[$newPrefix] = $value;
69 10
            $config += $this->expand($value, null, $newPrefix);
70
        }
71 10
        if ($prefix)
72 8
            $config[$prefix] = $array;
73 10
        return $config;
74
    }
75
76
}
77