Completed
Pull Request — master (#75)
by Josh
02:50
created

Config   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 76
ccs 16
cts 16
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setPath() 0 4 1
A loadConfigFile() 0 11 2
A get() 0 8 2
1
<?php
2
3
4
namespace Zewa\Config;
5
6
7
class Config
8
{
9
    /**
10
     * Loaded Configuration Items
11
     *
12
     * @var array
13
     */
14
    protected $configuration = [];
15
16
    /**
17
     * Path to Configuration Folder
18
     *
19
     * @var string
20
     */
21
    protected $path;
22
23
    /**
24
     * Configuration file extension
25
     */
26
    const CONFIG_FILE_EXTENSION = ".php";
27
28
    /**
29
     * Config constructor.
30
     *
31
     * @param $configFolderPath
32
     */
33 2
    public function __construct($configFolderPath)
34
    {
35 2
        $this->setPath($configFolderPath);
36 2
    }
37
38
    /**
39
     * Sets the configuration folder path
40
     *
41
     * @param $path
42
     */
43 2
    protected function setPath($path)
44
    {
45 2
        $this->path = $path;
46 2
    }
47
48
    /**
49
     * Loads a configuration file in to memory
50
     *
51
     * @param $key
52
     *
53
     * @throws ConfigException
54
     */
55 2
    protected function loadConfigFile($key)
56
    {
57 2
        if (!file_exists($this->path . "/" . $key . Config::CONFIG_FILE_EXTENSION)) {
58 1
            throw new ConfigException(
59
                'Configuration file does not exist: '
60 1
                . $this->path . "/" . $key . Config::CONFIG_FILE_EXTENSION
61
            );
62
        }
63
64 1
        $this->configuration[$key] = require $this->path . "/" . $key . Config::CONFIG_FILE_EXTENSION;
65 1
    }
66
67
    /**
68
     * Get Configuration Item
69
     *
70
     * @param $key
71
     *
72
     * @return mixed
73
     */
74 2
    public function get($key)
75
    {
76 2
        if (!isset($this->configuration[$key])) {
77 2
            $this->loadConfigFile($key);
78
        }
79
80 1
        return $this->configuration[$key];
81
    }
82
}