Completed
Push — master ( 29ebdf...575bfc )
by Ehsan
03:00
created

AbstractConfig::set()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 8
cp 0.75
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 5
nop 3
crap 4.25
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
     * @throws \Exception
21
     *
22
     * @return mixed
23
     */
24 108
    public function get($key, $replacements = [], $plugin = null)
25
    {
26 108
        $configs = static::$configs;
27
28
        // overwrite $configs if $plugin is presented
29 108
        if ($plugin !== null) {
30
            try {
31 3
                $configs = $this->getPluginConfigs($plugin);
32 1
            } catch (\Exception $e) {
33 1
                throw $e;
34
            }
35
        }
36
37 107
        if (!array_key_exists($key, $configs)) {
38 1
            throw new \Exception("Key: '{$key}' does not exist in configs");
39
        }
40
41 106
        $found = $configs[$key];
42
43 106
        return (new StringUtility())->applyReplacements($found, $replacements);
44
    }
45
46
    /**
47
     * @param $plugin
48
     *
49
     * @return self
50
     * @throws \Exception
51
     */
52 3
    private function getPluginConfigObject($plugin)
53
    {
54 3
        $pluginConfigClass = __NAMESPACE__.'\\plugin\\'.strtolower($plugin)
55 3
            .'\\'.ucfirst($plugin).'Config';
56
57 3
        if (!class_exists($pluginConfigClass)) {
58 1
            throw new \Exception("Config file: '{$pluginConfigClass}.php' does not exist");
59
        }
60
61 2
        return new $pluginConfigClass();
62
    }
63
64
    /**
65
     * @param $plugin
66
     *
67
     * @return array
68
     * @throws \Exception
69
     */
70 3
    public function getPluginConfigs($plugin)
71
    {
72
        try {
73 3
            return $this->getPluginConfigObject($plugin)->getConfigs();
74 1
        } catch (\Exception $e) {
75 1
            throw $e;
76
        }
77
    }
78
79
    /**
80
     * @return array
81
     */
82 2
    public function getConfigs()
83
    {
84 2
        return static::$configs;
85
    }
86
87
    /**
88
     * @param      $key
89
     * @param      $value
90
     * @param null $plugin
91
     *
92
     * @throws \Exception
93
     */
94 37
    public function set($key, $value, $plugin = null)
95
    {
96 37
        if ($plugin !== null) {
97
            try {
98 1
                $this->getPluginConfigObject($plugin)->set($key, $value);
99
            } catch (\Exception $e) {
100
                throw $e;
101
            }
102
        }
103
104 37
        is_array($key) ? (new ArrayUtility())->setNestedArrayValue(static::$configs, $key, $value)
105 20
            : static::$configs[$key] = $value;
106 37
    }
107
}
108