SettingsManager   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 276
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 276
ccs 89
cts 89
cp 1
rs 10
wmc 23
lcom 1
cbo 5

14 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 12 2
A all() 0 10 2
A delete() 0 21 3
A config() 0 4 1
A getChanges() 0 12 1
A grabDomain() 0 10 2
A loadData() 0 9 2
A __construct() 0 5 1
A getDefaultDomain() 0 4 1
A get() 0 8 1
A set() 0 15 2
A reset() 0 10 2
A save() 0 8 1
A checkLoaded() 0 8 2
1
<?php namespace Arcanesoft\Settings;
2
3
use Arcanedev\Support\Collection;
4
use Arcanesoft\Settings\Helpers\Arr;
5
use Arcanesoft\Settings\Helpers\Comparator;
6
use Arcanesoft\Settings\Models\Setting;
7
use Illuminate\Contracts\Cache\Repository as Cache;
8
9
/**
10
 * Class     SettingsManager
11
 *
12
 * @package  Arcanesoft\Settings
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class SettingsManager implements Contracts\Settings
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Properties
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    /**
22
     * The settings data.
23
     *
24
     * @var \Arcanedev\Support\Collection
25
     */
26
    protected $data;
27
28
    /**
29
     * Whether the store has changed since it was last loaded.
30
     *
31
     * @var bool
32
     */
33
    protected $unsaved = false;
34
35
    /**
36
     * Whether the settings data are loaded.
37
     *
38
     * @var bool
39
     */
40
    protected $loaded = false;
41
42
    /** @var  \Arcanesoft\Settings\Stores\EloquentStore  */
43
    private $store;
44
45
    /* ------------------------------------------------------------------------------------------------
46
     |  Constructor
47
     | ------------------------------------------------------------------------------------------------
48
     */
49
    /**
50
     * SettingsManager constructor.
51
     *
52
     * @param  \Arcanesoft\Settings\Models\Setting     $model
53
     * @param  \Illuminate\Contracts\Cache\Repository  $cache
54
     */
55 30
    public function __construct(Setting $model, Cache $cache)
56
    {
57 30
        $this->store = new Stores\EloquentStore($model, $cache);
58 30
        $this->data  = new Collection;
59 30
    }
60
61
    /* ------------------------------------------------------------------------------------------------
62
     |  Getters & Setters
63
     | ------------------------------------------------------------------------------------------------
64
     */
65
    /**
66
     * Get the settings default domain.
67
     *
68
     * @return string
69
     */
70 27
    protected function getDefaultDomain()
71
    {
72 27
        return $this->config('default-domain', 'default');
73
    }
74
75
    /**
76
     * Get the config value by key.
77
     *
78
     * @param  string  $key
79
     * @param  mixed   $default
80
     *
81
     * @return mixed
82
     */
83 27
    private function config($key, $default = null)
84
    {
85 27
        return config("arcanesoft.settings.$key", $default);
86
    }
87
88
    /* ------------------------------------------------------------------------------------------------
89
     |  Main Functions
90
     | ------------------------------------------------------------------------------------------------
91
     */
92
    /**
93
     * Get a setting by key.
94
     *
95
     * @param  string      $key
96
     * @param  mixed|null  $default
97
     *
98
     * @return mixed
99
     */
100 18
    public function get($key, $default = null)
101
    {
102 18
        $this->checkLoaded();
103
104 18
        $domain = $this->grabDomain($key);
105
106 18
        return Arr::get($this->data->get($domain, []), $key, $default);
107
    }
108
109
    /**
110
     * Set a setting.
111
     *
112
     * @param  string  $key
113
     * @param  mixed   $value
114
     */
115 24
    public function set($key, $value)
116
    {
117 24
        $this->checkLoaded();
118
119 24
        $domain = $this->grabDomain($key);
120 24
        $data   = [];
121
122 24
        if ($this->data->has($domain)) {
123 12
            $data = $this->data->get($domain);
124 8
        }
125
126 24
        Arr::set($data, $key, $value);
127
128 24
        $this->data->put($domain, $data);
129 24
    }
130
131
    /**
132
     * Check if a setting exists by the key.
133
     *
134
     * @param  string  $key
135
     *
136
     * @return bool
137
     */
138 18
    public function has($key)
139
    {
140 18
        $this->checkLoaded();
141
142 18
        $domain = $this->grabDomain($key);
143
144 18
        if ( ! $this->data->has($domain)) {
145 9
            return false;
146
        }
147
148 18
        return Arr::has($this->data->get($domain), $key);
149
    }
150
151
    /**
152
     * Get all the setting by a specific domain.
153
     *
154
     * @param  string|null  $domain
155
     *
156
     * @return array
157
     */
158 15
    public function all($domain = null)
159
    {
160 15
        $this->checkLoaded();
161
162 15
        if (is_null($domain)) {
163 12
            $domain = $this->getDefaultDomain();
164 8
        }
165
166 15
        return $this->data->get($domain, []);
167
    }
168
169
    /**
170
     * Delete a setting.
171
     *
172
     * @param  string  $key
173
     */
174 6
    public function delete($key)
175
    {
176 6
        $this->checkLoaded();
177
178 6
        $domain = $this->getDefaultDomain();
179
180 6
        if (str_contains($key, '::')) {
181 3
            list($domain, $key) = explode('::', $key);
182 2
        }
183
184 6
        $data = $this->data->get($domain, []);
185 6
        Arr::forget($data, $key);
186 6
        $data = array_filter($data);
187
188 6
        if (empty($data)) {
189 6
            $this->data->forget($domain);
190 4
        }
191
        else {
192 3
            $this->data->put($domain, $data);
193
        }
194 6
    }
195
196
    /**
197
     * Reset/Delete all the settings.
198
     *
199
     * @param  string|null  $domain
200
     */
201 3
    public function reset($domain = null)
202
    {
203 3
        $this->checkLoaded();
204
205 3
        if (is_null($domain)) {
206 3
            $domain = $this->getDefaultDomain();
207 2
        }
208
209 3
        $this->data->forget($domain);
210 3
    }
211
212
    /**
213
     * Save the settings.
214
     */
215 21
    public function save()
216
    {
217 21
        $changes = $this->getChanges(
218 21
            $saved = $this->store->all()
219 14
        );
220
221 21
        $this->store->save($saved, $changes);
222 21
    }
223
224
    /**
225
     * Get the changes.
226
     *
227
     * @param  \Illuminate\Database\Eloquent\Collection  $saved
228
     *
229
     * @return array
230
     */
231 21
    private function getChanges($saved)
232
    {
233 21
        return Comparator::compare(
234
            $this->data->map(function (array $settings) {
235 18
                return Arr::dot($settings);
236 21
            })->toArray(),
237 21
            $saved->groupBy('domain')->map(function($item) {
238
                /** @var  \Illuminate\Database\Eloquent\Collection  $item */
239 12
                return $item->pluck('casted_value', 'key');
240 21
            })->toArray()
241 14
        );
242
    }
243
244
    /**
245
     * Grab the settings domain name from the key.
246
     *
247
     * @param  string  $key
248
     *
249
     * @return string
250
     */
251 24
    private function grabDomain(&$key)
252
    {
253 24
        $domain = $this->getDefaultDomain();
254
255 24
        if (str_contains($key, '::')) {
256 6
            list($domain, $key) = explode('::', $key);
257 4
        }
258
259 24
        return $domain;
260
    }
261
262
    /* ------------------------------------------------------------------------------------------------
263
     |  Other Functions
264
     | ------------------------------------------------------------------------------------------------
265
     */
266
    /**
267
     * Check if the data is loaded
268
     */
269 27
    private function checkLoaded()
270
    {
271 27
        if ( ! $this->loaded) {
272 27
            $this->data->reset();
273 27
            $this->loadData();
274 27
            $this->loaded = true;
275 18
        }
276 27
    }
277
278
    /**
279
     * Load the data.
280
     */
281 27
    private function loadData()
282
    {
283 27
        foreach ($this->store->all() as $setting) {
284
            /** @var  \Arcanesoft\Settings\Models\Setting  $setting */
285 18
            $data = $this->data->get($setting->domain, []);
286 18
            Arr::set($data, $setting->key, $setting->casted_value);
287 18
            $this->data->put($setting->domain, $data);
288 18
        }
289 27
    }
290
}
291