Passed
Push — 4.9 ( 8f0f3a...f6adb4 )
by Mikhail
01:57
created

Config   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 25
dl 0
loc 59
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getOr() 0 11 3
A get() 0 8 3
A set() 0 3 1
A getAll() 0 3 1
A __construct() 0 15 2
A getSystemKeys() 0 3 1
1
<?php
2
3
class Config
4
{
5
    private $params = [];
6
    private $systemKeys = [];
7
8
    function __construct(array $arguments, array $defaults = [])
9
    {
10
        $this->set('path', ROOT_DIR);
11
12
        $self = $this;
13
        array_walk($defaults, function($setting, $key) use ($self) {
14
            $self->set(
15
                $key,
16
                $setting
17
            );
18
            $self->systemKeys[] = $key;
19
        });
20
        
21
        foreach($arguments as $key => $value) {
22
            $this->set($key, $value);
23
        }
24
    }
25
26
    public function set(string $key, $param): void
27
    {
28
        $this->params[$key] = $param;
29
    }
30
31
    public function get(string $key)
32
    {
33
        $result = @$this->params[$key] ?: null;
34
        $config = $this;
35
36
        return !is_string($result) ? $result : preg_replace_callback('/(\$\{([\w\.-_]+)\})/', function($matches) use ($config) {
37
            return $config->get($matches[2]);
38
        }, $result);
39
    }
40
41
    public function getOr(string ...$keys)
42
    {
43
        foreach($keys as $key) {
44
            $value = $this->get($key);
45
46
            if ($value) {
47
                return $value;
48
            }
49
        }
50
51
        return null;
52
    }
53
54
    public function getAll(): array
55
    {
56
        return $this->params;
57
    }
58
59
    public function getSystemKeys(): array
60
    {
61
        return $this->systemKeys;
62
    }
63
}
64