SettingsManger   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 42
ccs 20
cts 20
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A cacheKey() 0 4 1
A all() 0 4 1
A set() 0 4 1
A get() 0 12 3
A forget() 0 4 1
A flush() 0 6 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