Passed
Push — master ( 7a3107...5301ae )
by Ehsan
02:53
created

AbstractConfig::getConfigs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Botonomous;
4
5
use Botonomous\utility\ArrayUtility;
6
use Botonomous\utility\StringUtility;
7
8
/**
9
 * Class AbstractConfig.
10
 */
11
abstract class AbstractConfig
12
{
13
    protected static $configs;
14
15
    /**
16
     * @param       $key
17
     * @param array $replacements
18
     * @param null  $plugin
19
     *
20
     * @return mixed
21
     * @throws \Exception
22
     */
23 108
    public function get($key, $replacements = [], $plugin = null)
24
    {
25 108
        $configs = static::$configs;
26
27
        // overwrite $configs if $plugin is presented
28 108
        if ($plugin !== null) {
29
            try {
30 2
                $configs = $this->getPluginConfigs($plugin);
31 1
            } catch (\Exception $e) {
32 1
                throw $e;
33
            }
34
        }
35
36 107
        if (!array_key_exists($key, $configs)) {
37 1
            throw new \Exception("Key: '{$key}' does not exist in configs");
38
        }
39
40 106
        $found = $configs[$key];
41
42 106
        return (new StringUtility())->applyReplacements($found, $replacements);
43
    }
44
45 2
    public function getPluginConfigs($plugin)
46
    {
47 2
        $pluginConfigClass = __NAMESPACE__.'\\plugin\\'.strtolower($plugin)
48 2
            .'\\'.ucfirst($plugin).'Config';
49
50 2
        if (!class_exists($pluginConfigClass)) {
51 1
            throw new \Exception("Config file: '{$pluginConfigClass}.php' does not exist");
52
        }
53
54 1
        $pluginConfigObject = new $pluginConfigClass();
55 1
        if (!$pluginConfigObject instanceof self) {
56
            throw new \Exception("Class: '{$pluginConfigClass}' must extend BaseConfig");
57
        }
58
59 1
        return $pluginConfigObject->getConfigs();
60
    }
61
62 1
    public function getConfigs()
63
    {
64 1
        return static::$configs;
65
    }
66
67
    /**
68
     * @param $key
69
     * @param $value
70
     */
71 37
    public function set($key, $value)
72
    {
73 37
        is_array($key) ? (new ArrayUtility())->setNestedArrayValue(static::$configs, $key, $value)
74 20
            : static::$configs[$key] = $value;
75 37
    }
76
}
77