Test Failed
Push — master ( 8c2089...05126c )
by Mr
02:20
created

Config::load()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 9
nop 1
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace DrMVC\Config;
4
5
class Config implements ConfigInterface
6
{
7
    /**
8
     * Array with all parameters
9
     * @var array
10
     */
11
    private $_config = [];
12
13
    /**
14
     * Config constructor.
15
     * @param   null $autoload - File with array or array for auto loading
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $autoload is correct as it would always require null to be passed?
Loading history...
16
     */
17
    public function __construct($autoload = null)
18
    {
19
        if (!empty($autoload)) {
20
            // Read file from filesystem
21
            if (is_string($autoload)) {
22
                $this->load("$autoload");
23
            }
24
            // Parse parameters if array
25
            if (is_array($autoload)) {
26
                $this->setter($autoload);
27
            }
28
        }
29
    }
30
31
    /**
32
     * Load configuration file, show config path if needed
33
     *
34
     * @param   string $path - Path to file with array
35
     * @return  ConfigInterface
36
     */
37
    public function load(string $path): ConfigInterface
38
    {
39
        try {
40
            if (!file_exists($path)) {
41
                throw new ConfigException("Configuration file \"$path\" is not found");
42
            }
43
            if (!is_readable($path)) {
44
                throw new ConfigException("Configuration file \"$path\" is not readable");
45
            }
46
            $parameters = include "$path";
47
48
            if (!is_array($parameters)) {
49
                throw new ConfigException("Passed parameters is not array");
50
            }
51
            $this->setter($parameters);
52
53
        } catch (ConfigException $e) {
54
            // Error message implemented in exception
55
        }
56
        return $this;
57
    }
58
59
    /**
60
     * Put keys from array of parameters into internal array
61
     *
62
     * @param   array $parameters
63
     */
64
    private function setter(array $parameters)
65
    {
66
        // Parse array and set values
67
        array_map(
68
            function ($key, $value) {
69
                $this->set($key, $value);
70
            },
71
            array_keys($parameters),
72
            $parameters
73
        );
74
    }
75
76
    /**
77
     * Set some parameter of configuration
78
     *
79
     * @param   string $key
80
     * @param   mixed $value
81
     * @return  ConfigInterface
82
     */
83
    public function set(string $key, $value): ConfigInterface
84
    {
85
        $this->_config[$key] = $value;
86
        return $this;
87
    }
88
89
    /**
90
     * Get single parameter by name, or all available parameters
91
     *
92
     * @param   string|null $key
93
     * @return  mixed
94
     */
95
    public function get(string $key = null)
96
    {
97
        return empty($key)
98
            ? $this->_config
99
            : $this->_config[$key];
100
    }
101
102
    /**
103
     * Remove single value or clean config
104
     *
105
     * @param   string|null $key
106
     * @return  ConfigInterface
107
     */
108
    public function clean(string $key = null): ConfigInterface
109
    {
110
        $this->_config = [];
111
        return $this;
112
    }
113
}