HasSettings   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
getKey() 0 1 ?
A settingsCacheKey() 0 4 1
A allSettings() 0 4 1
A setSettings() 0 4 1
A getSettings() 0 12 3
A forgetSettings() 0 4 1
A flushSettings() 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
trait HasSettings
9
{
10
    abstract public function getKey();
11
12 14
    public function settingsCacheKey($key)
13
    {
14 14
        return config('settings.cache_prefix') . $key . '_' . $this->getKey();
15
    }
16
17 8
    public function allSettings()
18
    {
19 8
        return Setting::where('owner_id', $this->getKey())->pluck('value', 'key');
20
    }
21
22 20
    public function setSettings($key, $value)
23
    {
24 20
        Setting::updateOrCreate(['key' => $key, 'owner_id' => $this->getKey()], ['value' => $value,]);
25 20
    }
26
27 6
    public function getSettings($key, $default = null)
28
    {
29 6
        $cache_key = $this->settingsCacheKey($key);
30
31 6
        if (Cache::has($cache_key)) {
32 4
            return Cache::get($cache_key);
33
        }
34
35 2
        $value = Setting::where('key', $key)->where('owner_id', $this->getKey())->pluck('value')->first();
36
37 2
        return $value ? $value : $default;
38
    }
39
40 2
    public function forgetSettings($key)
41
    {
42 2
        Setting::where('key', $key)->where('owner_id', $this->getKey())->first()->delete();
43 2
    }
44
45
    public function flushSettings()
46
    {
47 2
        Setting::where('owner_id', $this->getKey())->each(function ($item) {
48 2
            $item->delete();
49 2
        });
50 2
    }
51
}
52