Completed
Push — master ( 62da11...a2f223 )
by
unknown
04:30 queued 01:37
created

Config::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
namespace Zewa;
3
4
use Zewa\Exception\ConfigException;
5
6
class Config
7
{
8
    /**
9
     * Loaded Configuration Items
10
     *
11
     * @var array
12
     */
13
    protected $configuration = [];
14
15
    /**
16
     * Path to Configuration Folder
17
     *
18
     * @var string
19
     */
20
    protected $path;
21
22
    /**
23
     * Configuration file extension
24
     */
25
    const CONFIG_FILE_EXTENSION = ".php";
26
27
    /**
28
     * Config constructor.
29
     *
30
     * @param $configFolderPath
31
     */
32 27
    public function __construct($configFolderPath = null)
33
    {
34 27
        if (is_null($configFolderPath)) {
35 2
            $configFolderPath = APP_PATH . DIRECTORY_SEPARATOR . 'Config';
36
        }
37
38 27
        $this->setPath($configFolderPath);
39 27
    }
40
41
    /**
42
     * Sets the configuration folder path
43
     *
44
     * @param $path
45
     */
46 27
    protected function setPath($path)
47
    {
48 27
        $this->path = $path;
49 27
    }
50
51
    /**
52
     * Loads a configuration file in to memory
53
     *
54
     * @param $key
55
     * @throws ConfigException when file is missing
56
     */
57 26
    protected function loadConfigFile(string $key)
58
    {
59 26
        $key = strtolower($key);
60 26
        $filename = $this->path . DIRECTORY_SEPARATOR . ucfirst($key) . Config::CONFIG_FILE_EXTENSION;
61
62 26
        if (file_exists($filename)) {
63 25
            $this->configuration[$key] = require $filename;
64 25
            return;
65
        }
66
67 1
        throw new ConfigException('Unable to load resource: ' . $filename);
68
    }
69
70
    /**
71
     * Get Configuration Item
72
     *
73
     * @param $key
74
     *
75
     * @return \stdClass
76
     * @throws ConfigException when config file not found
77
     */
78 27
    public function get(string $key)
79
    {
80 27
        $key = strtolower($key);
81
82 27
        if (empty($this->configuration[$key])) {
83 26
            $this->loadConfigFile($key);
84
        }
85
86 26
        return $this->configuration[$key];
87
    }
88
89
    /**
90
     * @param string $key
91
     * @param $value mixed array|string
92
     */
93 1
    public function set(string $key, $value)
94
    {
95 1
        $key = strtolower($key);
96 1
        $this->configuration[$key] = $value;
97 1
    }
98
}
99