Settings   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 70.79%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 29
c 6
b 0
f 1
lcom 1
cbo 4
dl 0
loc 245
ccs 63
cts 89
cp 0.7079
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 13 3
A delete() 0 9 2
A deleteAll() 0 4 1
A activate() 0 9 2
A clearCache() 0 11 3
A getRawConfig() 0 16 4
B get() 0 23 5
A has() 0 9 3
A set() 0 15 4
A deactivate() 0 9 2
1
<?php
2
3
/**
4
 * @link http://phe.me
5
 * @copyright Copyright (c) 2014 Pheme
6
 * @license MIT http://opensource.org/licenses/MIT
7
 */
8
9
namespace pheme\settings\components;
10
11
use yii\base\Component;
12
use yii\caching\Cache;
13
use Yii;
14
15
/**
16
 * @author Aris Karageorgos <[email protected]>
17
 */
18
class Settings extends Component
19
{
20
    /**
21
     * @var string settings model. Make sure your settings model calls clearCache in the afterSave callback
22
     */
23
    public $modelClass = 'pheme\settings\models\BaseSetting';
24
25
    /**
26
     * Model to for storing and retrieving settings
27
     * @var \pheme\settings\models\SettingInterface
28
     */
29
    protected $model;
30
31
    /**
32
     * @var Cache|string the cache object or the application component ID of the cache object.
33
     * Settings will be cached through this cache object, if it is available.
34
     *
35
     * After the Settings object is created, if you want to change this property,
36
     * you should only assign it with a cache object.
37
     * Set this property to null if you do not want to cache the settings.
38
     */
39
    public $cache = 'cache';
40
41
    /**
42
     * @var Cache|string the front cache object or the application component ID of the front cache object.
43
     * Front cache will be cleared through this cache object, if it is available.
44
     *
45
     * After the Settings object is created, if you want to change this property,
46
     * you should only assign it with a cache object.
47
     * Set this property to null if you do not want to clear the front cache.
48
     */
49
    public $frontCache;
50
51
    /**
52
     * To be used by the cache component.
53
     *
54
     * @var string cache key
55
     */
56
    public $cacheKey = 'pheme/settings';
57
58
    /**
59
     * Holds a cached copy of the data for the current request
60
     *
61
     * @var mixed
62
     */
63
    private $_data = null;
64
65
    /**
66
     * Initialize the component
67
     *
68
     * @throws \yii\base\InvalidConfigException
69
     */
70 22
    public function init()
71
    {
72 22
        parent::init();
73
74 22
        $this->model = new $this->modelClass;
75
76 22
        if (is_string($this->cache)) {
77 22
            $this->cache = Yii::$app->get($this->cache, false);
78 22
        }
79 22
        if (is_string($this->frontCache)) {
80
            $this->frontCache = Yii::$app->get($this->frontCache, false);
81
        }
82 22
    }
83
84
    /**
85
     * Get's the value for the given key and section.
86
     * You can use dot notation to separate the section from the key:
87
     * $value = $settings->get('section.key');
88
     * and
89
     * $value = $settings->get('key', 'section');
90
     * are equivalent
91
     *
92
     * @param $key
93
     * @param string|null $section
94
     * @param string|null $default
95
     * @return mixed
96
     */
97 4
    public function get($key, $section = null, $default = null)
98
    {
99 3
        if (is_null($section)) {
100 1
            $pieces = explode('.', $key, 2);
101 1
            if (count($pieces) > 1) {
102 1
                $section = $pieces[0];
103 1
                $key = $pieces[1];
104 1
            } else {
105
                $section = '';
106
            }
107 1
        }
108
109 3
        $data = $this->getRawConfig();
110
111 3
        if (isset($data[$section][$key][0])) {
112 3
            if (in_array($data[$section][$key][1], ['boolean', 'bool', 'integer', 'int', 'float', 'string', 'array'])) {
113 4
                settype($data[$section][$key][0], $data[$section][$key][1]);
114 4
            }
115 4
        } else {
116
            $data[$section][$key][0] = $default;
117
        }
118 3
        return $data[$section][$key][0];
119
    }
120
121
    /**
122
     * Checks to see if a setting exists.
123
     * If $searchDisabled is set to true, calling this function will result in an additional query.
124
     * @param $key
125
     * @param string|null $section
126
     * @param boolean $searchDisabled
127
     * @return boolean
128
     */
129 1
    public function has($key, $section = null, $searchDisabled = false)
130
    {
131 1
        if ($searchDisabled) {
132 1
            $setting = $this->model->findSetting($key, $section);
133
        } else {
134 1
            $setting = $this->get($key, $section);
135
        }
136 1
        return is_null($setting) ? false : true;
137
    }
138
139
    /**
140
     * @param $key
141
     * @param $value
142
     * @param null $section
143
     * @param null $type
144
     * @return boolean
145
     */
146 1
    public function set($key, $value, $section = null, $type = null)
147
    {
148 1
        if (is_null($section)) {
149
            $pieces = explode('.', $key);
150
            $section = $pieces[0];
151
            $key = $pieces[1];
152
        }
153
154 1
        if ($this->model->setSetting($section, $key, $value, $type)) {
155 1
            if ($this->clearCache()) {
156 1
                return true;
157
            }
158
        }
159 1
        return false;
160 1
    }
161
162
    /**
163
     * Deletes a setting
164
     *
165
     * @param $key
166
     * @param null|string $section
167
     * @return bool
168
     */
169 1
    public function delete($key, $section = null)
170
    {
171 1
        if (is_null($section)) {
172
            $pieces = explode('.', $key);
173
            $section = $pieces[0];
174
            $key = $pieces[1];
175
        }
176 1
        return $this->model->deleteSetting($section, $key);
177
    }
178
179
    /**
180
     * Deletes all setting. Be careful!
181
     *
182
     * @return bool
183
     */
184 1
    public function deleteAll()
185
    {
186 1
        return $this->model->deleteAllSettings();
187
    }
188
189
    /**
190
     * Activates a setting
191
     *
192
     * @param $key
193
     * @param null|string $section
194
     * @return bool
195
     */
196 5
    public function activate($key, $section = null)
197
    {
198 5
        if (is_null($section)) {
199
            $pieces = explode('.', $key);
200
            $section = $pieces[0];
201
            $key = $pieces[1];
202
        }
203 5
        return $this->model->activateSetting($section, $key);
204
    }
205
206
    /**
207
     * Deactivates a setting
208
     *
209
     * @param $key
210
     * @param null|string $section
211
     * @return bool
212
     */
213 1
    public function deactivate($key, $section = null)
214
    {
215 1
        if (is_null($section)) {
216
            $pieces = explode('.', $key);
217
            $section = $pieces[0];
218
            $key = $pieces[1];
219
        }
220 1
        return $this->model->deactivateSetting($section, $key);
221
    }
222
223
    /**
224
     * Clears the settings cache on demand.
225
     * If you haven't configured cache this does nothing.
226
     *
227
     * @return boolean True if the cache key was deleted and false otherwise
228
     */
229 22
    public function clearCache()
230
    {
231 22
        $this->_data = null;
232 22
        if ($this->frontCache instanceof Cache) {
233
            $this->frontCache->delete($this->cacheKey);
234
        }
235 22
        if ($this->cache instanceof Cache) {
236 22
            return $this->cache->delete($this->cacheKey);
237
        }
238
        return true;
239
    }
240
241
    /**
242
     * Returns the raw configuration array
243
     *
244
     * @return array
245
     */
246 3
    public function getRawConfig()
247
    {
248 3
        if ($this->_data === null) {
249 3
            if ($this->cache instanceof Cache) {
250 3
                $data = $this->cache->get($this->cacheKey);
251 3
                if ($data === false) {
252 3
                    $data = $this->model->getSettings();
253 3
                    $this->cache->set($this->cacheKey, $data);
254 3
                }
255 3
            } else {
256
                $data = $this->model->getSettings();
257
            }
258 3
            $this->_data = $data;
259 3
        }
260 3
        return $this->_data;
261
    }
262
}
263