Setting::getValueFor()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Microboard\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Spatie\MediaLibrary\HasMedia;
8
use Spatie\MediaLibrary\InteractsWithMedia;
9
10
class Setting extends Model implements HasMedia
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: addMedia, addMediaConversion, clearMediaCollection, clearMediaCollectionExcept, copyMedia, getMedia, hasMedia, loadMedia, media, registerAllMediaConversions, registerMediaCollections, registerMediaConversions, shouldDeletePreservingMedia
Loading history...
11
{
12
    use InteractsWithMedia;
13
14
    /**
15
     * @var array
16
     */
17
    protected $fillable = [
18
        'group', 'key', 'title', 'value', 'type', 'options'
19
    ];
20
21
    /**
22
     * @var array
23
     */
24
    protected $casts = [
25
        'options' => 'array'
26
    ];
27
28
    /**
29
     * @param string $delimiter
30
     * @param string $default
31
     * @return string|null
32
     */
33
    public static function getValueFor($delimiter, $default = '')
34
    {
35
        $key = explode('.', $delimiter);
36
        $group = $key[0];
37
38
        if (! isset($key[1])) {
39
            throw new \InvalidArgumentException("There is no key provided!");
40
        }
41
        $key = $key[1];
42
43
        return optional(self::where('key', $key)->where('group', $group)->first())->value ?? $default;
44
    }
45
}
46