Completed
Push — dev ( 9049a0 )
by Amr
05:18
created

HasSettings::allSettings()   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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Merodiro\Settings;
4
5
use Illuminate\Support\Facades\Cache;
6
use Merodiro\Settings\Models\Setting;
7
8
9
trait HasSettings
10
{
11 4
    public function allSettings()
12
    {
13 4
        return Setting::where('owner_id', $this->id)->pluck('value', 'key');
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
14
    }
15
16 16
    public function setSettings($key, $value)
17
    {
18 16
        Setting::updateOrCreate(['key' => $key, 'owner_id' => $this->id], ['value' => $value,]);
19 16
    }
20
21 8 View Code Duplication
    public function getSettings($key, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
    {
23 8
        $cache_key = config('settings.cache_prefix') . $key . '-' . $this->id;
24 8
        $duration = config('settings.cache_duration');
25
26 8
        $value = Cache::remember($cache_key, $duration, function () use ($key) {
27 6
            return Setting::where('key', $key)->where('owner_id', $this->id)->pluck('value')->first();
28 8
        });
29
30 8
        return $value ? $value : $default;
31
    }
32
33 2
    public function forgetSettings($key)
34
    {
35 2
        Setting::where('key', $key)->where('owner_id', $this->id)->first()->delete();
36 2
    }
37
38
    public function flushSettings()
39
    {
40 2
        Setting::where('owner_id', $this->id)->each(function ($item) {
41 2
            $item->delete();
42 2
        });
43 2
    }
44
}
45