SettingsManger::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Merodiro\Settings;
4
5
use Illuminate\Support\Facades\Cache;
6
use Merodiro\Settings\Models\Setting;
7
8
class SettingsManger
9
{
10 14
    public function cacheKey($key)
11
    {
12 14
        return config('settings.cache_prefix') . $key . '_global';
13
    }
14
15 12
    public function all()
16
    {
17 12
        return Setting::whereNull('owner_id')->pluck('value', 'key');
18
    }
19
20 20
    public function set($key, $value)
21
    {
22 20
        Setting::updateOrCreate(['key' => $key, 'owner_id' => null], ['value' => $value]);
23 20
    }
24
25 6
    public function get($key, $default = null)
26
    {
27 6
        $cache_key = static::cacheKey($key);
28
29 6
        if (Cache::has($cache_key)) {
30 4
            return Cache::get($cache_key);
31
        }
32
33 2
        $value = Setting::where('key', $key)->whereNull('owner_id')->pluck('value')->first();
34
35 2
        return $value ? $value : $default;
36
    }
37
38 2
    public function forget($key)
39
    {
40 2
        Setting::where('key', $key)->whereNull('owner_id')->first()->delete();
41 2
    }
42
43
    public function flush()
44
    {
45 2
        Setting::whereNull('owner_id')->each(function ($item) {
46 2
            $item->delete();
47 2
        });
48 2
    }
49
}
50