Config   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 20
c 1
b 0
f 0
dl 0
loc 49
rs 10

5 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 11 2
1
<?php
2
3
class Config
4
{
5
    private $params = [];
6
7
    function __construct(array $arguments, 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($key, $setting);
14
        });
15
        
16
        foreach($arguments as $key => $value) {
17
            $this->set($key, $value);
18
        }
19
    }
20
21
    public function set(string $key, $param): void
22
    {
23
        $this->params[$key] = $param;
24
    }
25
26
    public function get(string $key)
27
    {
28
        $result = @$this->params[$key] ?: null;
29
        $config = $this;
30
31
        return !is_string($result) ? $result : preg_replace_callback('/(\$\{([\w\.-_]+)\})/', function($matches) use ($config) {
32
            return $config->get($matches[2]);
33
        }, $result);
34
    }
35
36
    public function getOr(string ...$keys)
37
    {
38
        foreach($keys as $key) {
39
            $value = $this->get($key);
40
41
            if ($value) {
42
                return $value;
43
            }
44
        }
45
46
        return null;
47
    }
48
49
    public function getAll(): array
50
    {
51
        return $this->params;
52
    }
53
}
54