Config::set()   A
last analyzed

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 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace PHPieces\ANZGateway;
4
5
class Config
6
{
7
    private static $baseDir = __DIR__."/../config/";
8
    private static $store = [];
9
10 27
    public static function load($path)
11
    {
12 27
        if (!self::loaded($path)) {
13 3
            self::loadToStore($path);
14
        }
15
16 27
        return self::loadFromStore($path);
17
    }
18
19 24
    public static function set($key, $val)
20
    {
21 24
        self::$store[$key] = $val;
22 24
    }
23
24 3
    private static function loadToStore($path)
25
    {
26 3
        $vars = explode(".", $path);
27 3
        $file = array_shift($vars);
28 3
        self::$store[$file] = include(self::$baseDir."{$file}.php");
29 3
    }
30
31 27
    private static function loadFromStore($path)
32
    {
33 27
        $vars = explode(".", $path);
34 27
        $file = array_shift($vars);
35 27
        $config = self::$store[$file];
36
37 27
        foreach ($vars as $var) {
38 24
            if (!is_array($config)) {
39
                return null;
40
            }
41 24
            if (!array_key_exists($var, $config)) {
42
                return null;
43
            }
44 24
            $config = $config[$var];
45
        }
46 27
        return $config;
47
    }
48
49 27
    private static function loaded($path)
50
    {
51 27
        $vars = explode(".", $path);
52 27
        $file = array_shift($vars);
53 27
        return isset(self::$store[$file]);
54
    }
55
}
56