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

Config::setPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
}