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

Config   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 96
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setPath() 0 4 1
A loadConfigFile() 0 12 2
A get() 0 13 3
A set() 0 5 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 = null)
33
    {
34
        if (is_null($configFolderPath)) {
35
            $configFolderPath = APP_PATH . DIRECTORY_SEPARATOR . 'Config';
36
        }
37
38
        $this->setPath($configFolderPath);
39
    }
40
41
    /**
42
     * Sets the configuration folder path
43
     *
44
     * @param $path
45
     */
46
    protected function setPath($path)
47
    {
48
        $this->path = $path;
49
    }
50
51
    /**
52
     * Loads a configuration file in to memory
53
     *
54
     * @param $key
55
     * @return bool
56
     */
57
    protected function loadConfigFile(string $key) : bool
58
    {
59
        $key = strtolower($key);
60
        $filename = $this->path . DIRECTORY_SEPARATOR . ucfirst($key) . Config::CONFIG_FILE_EXTENSION;
61
62
        if (file_exists($filename)) {
63
            $this->configuration[$key] = require $filename;
64
            return true;
65
        }
66
67
        return false;
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
    public function get(string $key)
79
    {
80
        $key = strtolower($key);
81
        $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...
82
83
        if (isset($this->configuration[$key]) || $this->loadConfigFile($key)) {
84
            $value = $this->configuration[$key];
85
        } else {
86
            throw new ConfigException($key . ' configuration file is missing.');
87
        }
88
89
        return $value;
90
    }
91
92
    /**
93
     * @param string $key
94
     * @param $value mixed array|string
95
     */
96
    public function set(string $key, $value)
97
    {
98
        $key = strtolower($key);
99
        $this->configuration[$key] = $value;
100
    }
101
}
102