Passed
Push — tailwind ( c8836f...b83e78 )
by Fèvre
05:03
created

Setting   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A castValue() 0 28 4
1
<?php
2
namespace Xetaravel\Models;
3
4
use Xetaravel\Models\Presenters\SettingPresenter;
5
6
class Setting extends Model
7
{
8
    use SettingPresenter;
9
10
    /**
11
     * All type with their labels. (Used in admin panel for radio buttons)
12
     */
13
    public const TYPES = [
14
        'value_int' => 'Value Integer',
15
        'value_str' => 'Value String',
16
        'value_bool' => 'Value Boolean'
17
    ];
18
19
    /**
20
     * The attributes that are mass assignable.
21
     *
22
     * @var array
23
     */
24
    protected $fillable = [
25
        'name',
26
        'value_int',
27
        'value_str',
28
        'value_bool',
29
        'value',
30
        'description',
31
        'last_updated_user_id',
32
        'is_deletable'
33
    ];
34
35
    /**
36
     * The accessors to append to the model's array form.
37
     *
38
     * @var array
39
     */
40
    protected $appends = [
41
        'value',
42
        'type'
43
    ];
44
45
    /**
46
     * Function to set the value regarding the type to the model and return it.
47
     *
48
     * @param string $value The value to assign.
49
     * @param string $type The type of the value.
50
     * @param Setting $model The model where the value will be assigned.
51
     *
52
     * @return mixed
53
     */
54
    protected static function castValue(string $value, string $type, Setting $model)
55
    {
56
        switch ($type) {
57
            case "value_int":
58
                $model->value_int = intval($value);
0 ignored issues
show
Bug introduced by
The property value_int does not seem to exist on Xetaravel\Models\Setting. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
59
                $model->value_bool = null;
0 ignored issues
show
Bug introduced by
The property value_bool does not seem to exist on Xetaravel\Models\Setting. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
60
                $model->value_str = null;
0 ignored issues
show
Bug introduced by
The property value_str does not seem to exist on Xetaravel\Models\Setting. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
61
                break;
62
63
            case "value_bool":
64
                $model->value_bool = boolval($value);
65
                $model->value_int = null;
66
                $model->value_str = null;
67
                break;
68
69
            case "value_str":
70
                $model->value_str = $value;
71
                $model->value_int = null;
72
                $model->value_bool = null;
73
                break;
74
75
            default:
76
                $model->value_str = $value;
77
                $model->value_int = null;
78
                $model->value_bool = null;
79
                break;
80
        }
81
        return $model;
82
    }
83
}
84