Passed
Push — main ( 5e57d5...ad0a12 )
by PRATIK
04:20
created

Setting::getActivitylogOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 1
f 0
1
<?php
2
3
namespace Pratiksh\Adminetic\Models\Admin;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\Cache;
7
use Spatie\Activitylog\Traits\LogsActivity;
8
use Spatie\Activitylog\LogOptions;
9
10
class Setting extends Model
11
{
12
    use LogsActivity;
0 ignored issues
show
introduced by
The trait Spatie\Activitylog\Traits\LogsActivity requires some properties which are not provided by Pratiksh\Adminetic\Models\Admin\Setting: $submitEmptyLogs, $logExceptAttributes, $attributeRawValues, $dontLogIfAttributesChangedOnly, $descriptionForEvent, $logOnlyDirty, $logFillable, $logUnguarded, $logAttributes
Loading history...
13
14
    protected $guarded = [];
15
16
    // Forget cache on updating or saving and deleting
17
    public static function boot()
18
    {
19
        parent::boot();
20
21
        static::saving(function () {
22
            self::cacheKey();
23
        });
24
25
        static::deleting(function () {
26
            self::cacheKey();
27
        });
28
    }
29
30
    // Cache Keys
31
    private static function cacheKey()
32
    {
33
        Cache::has('settings') ? Cache::forget('settings') : '';
34
    }
35
36
    // Logs
37
    protected static $logName = 'setting';
38
39
    public function getActivitylogOptions(): LogOptions
40
    {
41
        return LogOptions::defaults();
42
    }
43
44
    // Casts
45
    public $casts = [
46
        'setting_custom' => 'array',
47
        'setting_json' => 'array',
48
    ];
49
50
    // Appends
51
    public $appends = ['custom', 'value'];
52
53
    // Mutators
54
    public function setSettingNameAttribute($value)
55
    {
56
        $this->attributes['setting_name'] = strtolower(str_replace(' ', '_', $value));
57
    }
58
59
    public function setSettingGroupAttribute($value)
60
    {
61
        $this->attributes['setting_group'] = strtolower(str_replace(' ', '_', $value));
62
    }
63
64
    // Accessors
65
    public function getSettingNameAttribute($value)
66
    {
67
        return ucwords(str_replace('_', ' ', $value));
68
    }
69
70
    public function getSettingGroupAttribute($value)
71
    {
72
        return ucwords(str_replace('_', ' ', $value));
73
    }
74
75
    public function getSettingTypeAttribute($attribute)
76
    {
77
        return [
78
            1 => 'string',
79
            2 => 'integer',
80
            3 => 'text',
81
            4 => 'rich text',
82
            5 => 'switch',
83
            6 => 'checkbox',
84
            7 => 'select',
85
            8 => 'multiple',
86
            9 => 'tag',
87
            10 => 'image',
88
        ][$attribute];
89
    }
90
91
    public function getCustomAttribute()
92
    {
93
        return isset($this->setting_custom) ? json_decode($this->setting_custom) : null;
0 ignored issues
show
Bug introduced by
$this->setting_custom of type array is incompatible with the type string expected by parameter $json of json_decode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
        return isset($this->setting_custom) ? json_decode(/** @scrutinizer ignore-type */ $this->setting_custom) : null;
Loading history...
94
    }
95
96
    public function getValueAttribute()
97
    {
98
        if ($this->getRawOriginal('setting_type') == 1 || $this->getRawOriginal('setting_type') == 10) {
99
            return $this->string_value;
100
        } elseif ($this->getRawOriginal('setting_type') == 2 || $this->getRawOriginal('setting_type') == 6 || $this->getRawOriginal('setting_type') == 7) {
101
            return $this->integer_value;
102
        } elseif ($this->getRawOriginal('setting_type') == 3 || $this->getRawOriginal('setting_type') == 4) {
103
            return $this->text_value;
104
        } elseif ($this->getRawOriginal('setting_type') == 5) {
105
            return $this->boolean_value;
106
        } elseif ($this->getRawOriginal('setting_type') == 8 || $this->getRawOriginal('setting_type') == 9) {
107
            return $this->setting_json;
108
        } else {
109
            return null;
110
        }
111
    }
112
}
113