Completed
Push — master ( 366f5a...9cc24d )
by Alex
06:09
created

Settings::set()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
rs 9.8333
c 0
b 0
f 0
cc 3
nc 4
nop 4
crap 3.0123
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 29
    public function init()
78
    {
79 29
        parent::init();
80
81 29
        $this->model = new $this->modelClass;
82
83 29
        if (is_string($this->cache)) {
84 29
            $this->cache = Yii::$app->get($this->cache, false);
85 29
        }
86 29
        if (is_string($this->frontCache)) {
87
            $this->frontCache = Yii::$app->get($this->frontCache, false);
88
        }
89 29
    }
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 11
    public function get($key, $section = null, $default = null)
105
    {
106 3
        if (is_null($section)) {
107 1
            $pieces = explode('.', $key, 2);
108 1
            if (count($pieces) > 1) {
109 1
                $section = $pieces[0];
110 8
                $key = $pieces[1];
111 1
            } else {
112 8
                $section = '';
113 8
            }
114 9
        }
115
116 3
        $data = $this->getRawConfig();
117
118 11
        if (isset($data[$section][$key][0])) {
119
120 10
            $value = $data[$section][$key][0];
121 3
            $type = $data[$section][$key][1];
122
123
            //convert value to needed type
124 3
            if (in_array($type, ['object', 'boolean', 'bool', 'integer', 'int', 'float', 'string', 'array'])) {
125 3
                if ($this->autoDecodeJson && $type === 'object') {
126
                    $value = Json::decode($value);
127
                } else {
128 3
                    settype($value, $type);
129
                }
130 3
            }
131 3
        } else {
132
            $value = $default;
133 5
        }
134 5
        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 1
            $setting = $this->get($key, $section);
151
        }
152 1
        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 8
    public function set($key, $value, $section = null, $type = null)
163
    {
164 8
        if (is_null($section)) {
165 7
            $pieces = explode('.', $key);
166 7
            $section = $pieces[0];
167 7
            $key = $pieces[1];
168 7
        }
169
170 8
        if ($this->model->setSetting($section, $key, $value, $type)) {
171 8
            return true;
172
        }
173
        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
    public function getOrSet($key, $value, $section = null, $type = null)
187
    {
188
        if ($this->has($key, $section, true)) {
189
            return $this->get($key, $section);
190
        } else {
191
            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
            $pieces = explode('.', $key);
206
            $section = $pieces[0];
207
            $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
            $pieces = explode('.', $key);
233
            $section = $pieces[0];
234
            $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 1
    public function deactivate($key, $section = null)
247
    {
248 1
        if (is_null($section)) {
249
            $pieces = explode('.', $key);
250
            $section = $pieces[0];
251
            $key = $pieces[1];
252
        }
253 1
        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 29
    public function clearCache()
263
    {
264 29
        $this->_data = null;
265 29
        if ($this->frontCache instanceof Cache) {
266
            $this->frontCache->delete($this->cacheKey);
267
        }
268 29
        if ($this->cache instanceof Cache) {
269 29
            return $this->cache->delete($this->cacheKey);
270
        }
271
        return true;
272
    }
273
274
    /**
275
     * Returns the raw configuration array
276
     *
277
     * @return array
278
     */
279 3
    public function getRawConfig()
280
    {
281 3
        if ($this->_data === null) {
282 3
            if ($this->cache instanceof Cache) {
283 3
                $data = $this->cache->get($this->cacheKey);
284 3
                if ($data === false) {
285 3
                    $data = $this->model->getSettings();
286 3
                    $this->cache->set($this->cacheKey, $data);
287 3
                }
288 3
            } else {
289
                $data = $this->model->getSettings();
290
            }
291 3
            $this->_data = $data;
292 3
        }
293 3
        return $this->_data;
294
    }
295
}
296