Completed
Push — master ( ad5456...f93915 )
by Amine
10s
created

Config::get()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 4
nop 1
1
<?php namespace Tarsana\Command\Config;
2
3
use Tarsana\Command\Interfaces\Config\ConfigInterface;
4
5
/**
6
 * Stores and gets configuration.
7
 */
8
class Config implements ConfigInterface {
9
    /**
10
     * The raw configuration data.
11
     *
12
     * @var array
13
     */
14
    protected $data;
15
16
    public function __construct(array $data) {
17
        $this->data = $data;
18
    }
19
20
    /**
21
     * Gets a configuration value by path.
22
     */
23
    public function get(string $path = null)
24
    {
25
        if (null === $path)
26
            return $this->data;
27
        $keys = explode('.', $path);
28
        $value = $this->data;
29
        foreach ($keys as $key) {
30
            if (!is_array($value) || !array_key_exists($key, $value))
31
                throw new \Exception("Unable to find a configuration value with path '{$path}'");
32
            $value = $value[$key];
33
        }
34
        return $value;
35
    }
36
}
37