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

SettingsManger::cacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
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 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