Completed
Push — master ( 194f59...80cd65 )
by James Ekow Abaka
02:14
created

Data   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.56%

Importance

Changes 9
Bugs 0 Features 2
Metric Value
wmc 17
c 9
b 0
f 2
lcom 1
cbo 2
dl 0
loc 72
ccs 40
cts 41
cp 0.9756
rs 10

7 Methods

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