Completed
Push — dev ( cb6296...2e3e2f )
by Amr
03:50
created

SettingsManger   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 46
ccs 23
cts 23
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A cacheKey() 0 4 1
A all() 0 4 1
A set() 0 8 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 22
    public function cacheKey($key)
11
    {
12 22
        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
        $cache_key = static::cacheKey($key);
23 20
        $duration = config('settings.cache_duration');
24
25 20
        Setting::updateOrCreate(['key' => $key, 'owner_id' => null], ['value' => $value]);
26 20
        Cache::put($cache_key, $value, $duration);
27 20
    }
28
29 6
    public function get($key, $default = null)
30
    {
31 6
        $cache_key = static::cacheKey($key);
32
33 6
        if (Cache::has($cache_key)) {
34 4
            return Cache::get($cache_key);
35
        }
36
37 2
        $value = Setting::where('key', $key)->whereNull('owner_id')->pluck('value')->first();
38
39 2
        return $value ? $value : $default;
40
    }
41
42 2
    public function forget($key)
43
    {
44 2
        Setting::where('key', $key)->whereNull('owner_id')->first()->delete();
45 2
    }
46
47
    public function flush()
48
    {
49 2
        Setting::whereNull('owner_id')->each(function ($item) {
50 2
            $item->delete();
51 2
        });
52 2
    }
53
}
54