Passed
Branch 4.9 (cb955a)
by Mikhail
01:30
created

Config::__construct()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 23
c 1
b 0
f 0
rs 9.4888
cc 5
nc 3
nop 2
1
<?php
2
3
class Config
4
{
5
    private $params = [];
6
7
    function __construct(array $argv, array $defaults = [])
8
    {
9
        $this->set('path', ROOT_DIR);
10
11
        $self = $this;
12
        array_walk($defaults, function($setting, $key) use ($self) {
13
            $self->set(
14
                $key,
15
                $setting
16
            );
17
        });
18
19
        foreach($argv as $arg) {
20
            $e = explode('=', $arg);
21
            if (count($e) === 2) {
22
                $this->set($e[0], $e[1]);
23
            } else {
24
                $this->set($e[0], true);
25
            }
26
        }
27
28
        $this->set('generator', to_camel_case(@$argv[1] ?: ''));
29
        $this->set('command', @$argv[2] ?: '');
30
    }
31
32
    public function set(string $key, $param): void
33
    {
34
        $this->params[$key] = $param;
35
    }
36
37
    public function get(string $key)
38
    {
39
        $result = @$this->params[$key] ?: null;
40
        $config = $this;
41
42
        return !is_string($result) ? $result : preg_replace_callback('/(\$\{([\w\.-_]+)\})/', function($matches) use ($config) {
43
            return $config->get($matches[2]);
44
        }, $result);
45
    }
46
47
    public function getOr(string ...$keys)
48
    {
49
        $value = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
50
51
        foreach($keys as $key) {
52
            $value = $this->get($key);
53
54
            if ($value) {
55
                return $value;
56
            }
57
        }
58
59
        return null;
60
    }
61
62
    public function getAll(): array
63
    {
64
        return $this->params;
65
    }
66
}
67