Completed
Pull Request — master (#31)
by Igor
02:11
created

Settings::getAllBySection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace yii2mod\settings\components;
4
5
use Yii;
6
use yii\base\Component;
7
use yii\caching\Cache;
8
use yii\di\Instance;
9
use yii\helpers\ArrayHelper;
10
use yii2mod\settings\models\enumerables\SettingType;
11
12
/**
13
 * Class Settings
14
 *
15
 * @package yii2mod\settings\components
16
 */
17
class Settings extends Component
18
{
19
    /**
20
     * @var string setting model class name
21
     */
22
    public $modelClass = 'yii2mod\settings\models\SettingModel';
23
24
    /**
25
     * @var Cache|array|string the cache used to improve RBAC performance. This can be one of the followings:
26
     *
27
     * - an application component ID (e.g. `cache`)
28
     * - a configuration array
29
     * - a [[yii\caching\Cache]] object
30
     *
31
     * When this is not set, it means caching is not enabled
32
     */
33
    public $cache = 'cache';
34
35
    /**
36
     * @var string the key used to store settings data in cache
37
     */
38
    public $cacheKey = 'yii2mod-setting';
39
40
    /**
41
     * @var \yii2mod\settings\models\SettingModel setting model
42
     */
43
    protected $model;
44
45
    /**
46
     * @var array list of settings
47
     */
48
    protected $items;
49
50
    /**
51
     * @var mixed setting value
52
     */
53
    protected $setting;
54
55
    /**
56
     * Initialize the component
57
     */
58
    public function init()
59
    {
60
        parent::init();
61
62
        if ($this->cache !== null) {
63
            $this->cache = Instance::ensure($this->cache, Cache::class);
64
        }
65
66
        $this->model = Yii::createObject($this->modelClass);
67
    }
68
69
    /**
70
     * Get's all values in the specific section.
71
     *
72
     * @param string $section
73
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
74
     *
75
     * @return mixed
76
     */
77
    public function getAllBySection($section, $default = null)
78
    {
79
        $items = $this->getSettingsConfig();
80
81
        if (isset($items[$section])) {
82
            $this->setting = ArrayHelper::getColumn($items[$section], 'value');
83
        } else {
84
            $this->setting = $default;
85
        }
86
87
88
        return $this->setting;
89
    }
90
91
    /**
92
     * Get's the value for the given section and key.
93
     *
94
     * @param string $section
95
     * @param string $key
96
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
97
     *
98
     * @return mixed
99
     */
100
    public function get($section, $key, $default = null)
101
    {
102
        $items = $this->getSettingsConfig();
103
104
        if (isset($items[$section][$key])) {
105
            $this->setting = ArrayHelper::getValue($items[$section][$key], 'value');
106
            $type = ArrayHelper::getValue($items[$section][$key], 'type');
107
            $this->convertSettingType($type);
108
        } else {
109
            $this->setting = $default;
110
        }
111
112
        return $this->setting;
113
    }
114
115
    /**
116
     * Add a new setting or update an existing one.
117
     *
118
     * @param null $section
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $section is correct as it would always require null to be passed?
Loading history...
119
     * @param string $key
120
     * @param string $value
121
     * @param null $type
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $type is correct as it would always require null to be passed?
Loading history...
122
     *
123
     * @return bool
124
     */
125
    public function set($section, $key, $value, $type = null): bool
126
    {
127
        if ($this->model->setSetting($section, $key, $value, $type)) {
128
            if ($this->invalidateCache()) {
129
                return true;
130
            }
131
        }
132
133
        return false;
134
    }
135
136
    /**
137
     * Checking existence of setting
138
     *
139
     * @param string $section
140
     * @param string $key
141
     *
142
     * @return bool
143
     */
144
    public function has($section, $key): bool
145
    {
146
        $setting = $this->get($section, $key);
147
148
        return !empty($setting);
149
    }
150
151
    /**
152
     * Remove setting by section and key
153
     *
154
     * @param string $section
155
     * @param string $key
156
     *
157
     * @return bool
158
     */
159
    public function remove($section, $key): bool
160
    {
161
        if ($this->model->removeSetting($section, $key)) {
162
            if ($this->invalidateCache()) {
163
                return true;
164
            }
165
        }
166
167
        return false;
168
    }
169
170
    /**
171
     * Remove all settings
172
     *
173
     * @return int
174
     */
175
    public function removeAll(): int
176
    {
177
        return $this->model->removeAllSettings();
178
    }
179
180
    /**
181
     * Activates a setting
182
     *
183
     * @param string $key
184
     * @param string $section
185
     *
186
     * @return bool
187
     */
188
    public function activate($section, $key): bool
189
    {
190
        return $this->model->activateSetting($section, $key);
191
    }
192
193
    /**
194
     * Deactivates a setting
195
     *
196
     * @param string $key
197
     * @param string $section
198
     *
199
     * @return bool
200
     */
201
    public function deactivate($section, $key): bool
202
    {
203
        return $this->model->deactivateSetting($section, $key);
204
    }
205
206
    /**
207
     * Returns the settings config
208
     *
209
     * @return array
210
     */
211
    protected function getSettingsConfig(): array
212
    {
213
        if (!$this->cache instanceof Cache) {
214
            $this->items = $this->model->getSettings();
215
        } else {
216
            $cacheItems = $this->cache->get($this->cacheKey);
217
            if (!empty($cacheItems)) {
218
                $this->items = $cacheItems;
219
            } else {
220
                $this->items = $this->model->getSettings();
221
                $this->cache->set($this->cacheKey, $this->items);
222
            }
223
        }
224
225
        return $this->items;
226
    }
227
228
    /**
229
     * Invalidate the cache
230
     *
231
     * @return bool
232
     */
233
    public function invalidateCache(): bool
234
    {
235
        if ($this->cache !== null) {
236
            $this->cache->delete($this->cacheKey);
237
            $this->items = null;
238
        }
239
240
        return true;
241
    }
242
243
    /**
244
     * Set type for setting
245
     *
246
     * @param $type
247
     */
248
    protected function convertSettingType($type)
249
    {
250
        if ($type === SettingType::BOOLEAN_TYPE) {
251
            $this->setting = filter_var($this->setting, FILTER_VALIDATE_BOOLEAN);
252
        } else {
253
            settype($this->setting, $type);
254
        }
255
    }
256
}
257