Completed
Push — master ( fab37c...20384e )
by Alex
02:09
created

Settings::clearCache()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

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