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

AbstractConfig   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 97
ccs 28
cts 30
cp 0.9333
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 21 4
A getPluginConfigObject() 0 11 2
A getPluginConfigs() 0 8 2
A getConfigs() 0 4 1
A set() 0 13 4
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