Completed
Pull Request — master (#81)
by
unknown
03:23
created

Config::setPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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
    public function __construct($configFolderPath)
33
    {
34
        $this->setPath($configFolderPath);
35
    }
36
37
    /**
38
     * Sets the configuration folder path
39
     *
40
     * @param $path
41
     */
42
    protected function setPath($path)
43
    {
44
        $this->path = $path;
45
    }
46
47
    /**
48
     * Loads a configuration file in to memory
49
     *
50
     * @param $key
51
     * @return bool
52
     */
53
    protected function loadConfigFile(string $key) : bool
54
    {
55
        $key = strtolower($key);
56
        $filename = $this->path . DIRECTORY_SEPARATOR . ucfirst($key) . Config::CONFIG_FILE_EXTENSION;
57
58
        if (file_exists($filename)) {
59
            $this->configuration[$key] = require $filename;
60
            return true;
61
        }
62
63
        return false;
64
    }
65
66
    /**
67
     * Get Configuration Item
68
     *
69
     * @param $key
70
     *
71
     * @return \stdClass
72
     * @throws ConfigException when config file not found
73
     */
74
    public function get(string $key)
75
    {
76
        $key = strtolower($key);
77
        $value = [];
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
79
        if (isset($this->configuration[$key]) || $this->loadConfigFile($key)) {
80
            $value = $this->configuration[$key];
81
        } else {
82
            throw new ConfigException($key . ' configuration file is missing.');
83
        }
84
85
        return $value;
86
    }
87
88
    /**
89
     * @param string $key
90
     * @param $value mixed array|string
91
     */
92
    public function set(string $key, $value)
93
    {
94
        $key = strtolower($key);
95
        $this->configuration[$key] = $value;
96
    }
97
}
98