Completed
Push — master ( bb6be8...87747d )
by James Ekow Abaka
01:26
created

Config::setContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace ntentan\config;
4
5
class Config
6
{
7
8
    private $config;
9
    private $context = 'default';
10
    
11
    /**
12
     * Reads the configurations stored at a given path.
13
     * 
14
     * Path could either point to a directory or a specific file. In the case of
15
     * a file, the contents of the file are read into the config object. However,
16
     * in the case of a directory, the contents of all the files in the directory 
17
     * are read into the config file with configurations from each file prefixed with
18
     * the file name.
19
     *
20
     * @param string $path
21
     * @return void
22
     */
23 4
    public function readPath($path) {
24 4
        if (is_dir($path)) {
25 3
            $dir = new Directory($path);
26 3
            $this->config = ['default' => $this->expand($dir->parse())];
27 3
            foreach ($dir->getContexts() as $context) {
28 2
                $this->config[$context] = array_merge(
29 3
                    $this->config['default'], $this->expand($dir->parse($context))
30
                );
31
            }
32 1
        } else if (is_file($path)) {
33 1
            $this->config[$this->context] = $this->expand(File::read($path));
34
        } else {
35
            throw new \ntentan\utils\exceptions\FileNotFoundException($path);
36
        }
37 4
    }
38
39
    public function isKeySet($key) {
40
        return isset($this->config[$this->context][$key]);
41
    }
42
43 6
    public function get($key, $default = null) {
44 6
        return isset($this->config[$this->context][$key]) ? $this->config[$this->context][$key] : $default;
45
    }
46
47 1
    public function setContext($context) {
48 1
        $this->context = $context;
49 1
    }
50
51 3
    public function set($key, $value) {
52 3
        $keys = explode('.', $key);
53 3
        $this->config[$this->context] = $this->setValue($keys, $value, $this->config[$this->context]);
54 3
        $this->config[$this->context][$key] = $value;
55 3
        if (is_array($value)) {
56 1
            $this->config[$this->context] += $this->expand($value, $key);
57
        }
58 3
    }
59
60 3
    private function setValue($keys, $value, $config) {
61 3
        if (!empty($keys)) {
62 3
            $key = array_shift($keys);
63 3
            $config[$key] = $this->setValue($keys, $value, isset($config[$key]) ? $config[$key] : []);
64 3
            return $config;
65
        } else {
66 3
            return $value;
67
        }
68
    }
69
70 5
    private function expand($array, $prefix = null) {
71 5
        $config = [];
72 5
        if (!is_array($array))
73 5
            return $config;
74 5
        $dottedPrefix = $prefix ? "$prefix." : "";
75 5
        foreach ($array as $key => $value) {
76 5
            $newPrefix = $dottedPrefix . $key;
77 5
            $config[$newPrefix] = $value;
78 5
            $config += $this->expand($value, $newPrefix);
79
        }
80 5
        if ($prefix)
81 4
            $config[$prefix] = $array;
82 5
        return $config;
83
    }
84
85
}
86