Passed
Branch master (cd4548)
by Fèvre
19:36
created

Setting::castValue()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
nc 5
nop 2
dl 0
loc 27
rs 9.2888
c 1
b 0
f 0
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
     * The attributes that are mass assignable.
12
     *
13
     * @var array
14
     */
15
    protected $fillable = [
16
        'name',
17
        'value_int',
18
        'value_str',
19
        'value_bool',
20
        'value',
21
        'description',
22
        'last_updated_user_id'
23
    ];
24
25
26
    /**
27
     * The accessors to append to the model's array form.
28
     *
29
     * @var array
30
     */
31
    protected $appends = [
32
        'value'
33
    ];
34
35
    /**
36
     * The "booting" method of the model.
37
     *
38
     * @return void
39
     */
40
    protected static function boot()
41
    {
42
        parent::boot();
43
44
        // Get the type of the value before to save.
45
        static::updating(function ($model) {
46
            $model = self::castValue($model->new_value, $model);
47
48
            unset($model->new_value);
49
        });
50
    }
51
52
    private static function castValue($value, $model)
53
    {
54
        switch (gettype($value)) {
55
            case 'int':
56
            case 'integer':
57
                $model->value_int = $value;
58
                $model->value_str = null;
59
                $model->value_bool = null;
60
61
                return $model;
62
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
63
64
            case 'bool':
65
            case 'boolean':
66
                $model->value_int = null;
67
                $model->value_str = null;
68
                $model->value_bool = $value;
69
70
                return $model;
71
                break;
72
73
            default:
74
                $model->value_int = null;
75
                $model->value_str = $value;
76
                $model->value_bool = null;
77
78
                return $model;
79
        }
80
    }
81
}
82