Passed
Push — 5.0.0 ( 8a9c00...cfa35f )
by Fèvre
06:04
created

RoleForm::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Livewire\Forms;
6
7
use Illuminate\Validation\Rule;
8
use Livewire\Form;
9
use Spatie\Permission\Models\Role;
10
11
class RoleForm extends Form
12
{
13
    /**
14
     * The role to update.
15
     *
16
     * @var Role|null
17
     */
18
    public ?Role $role = null;
19
20
    /**
21
     * The name of the role.
22
     *
23
     * @var string|null
24
     */
25
    public ?string $name = null;
26
27
    /**
28
     * The color of the category.
29
     *
30
     * @var string|null
31
     */
32
    public ?string $color = null;
33
34
    /**
35
     * The level of the category.
36
     *
37
     * @var int|null
38
     */
39
    public ?int $level = null;
40
41
    /**
42
     * The description of the role.
43
     *
44
     * @var string|null
45
     */
46
    public ?string $description = null;
47
48
    /**
49
     * The permissions of the role.
50
     *
51
     * @var array
52
     */
53
    public array $permissions = [];
54
55
    protected function rules(): array
56
    {
57
        return [
58
            'name' => [
59
                'required',
60
                'min:3',
61
                Rule::unique('roles')->ignore($this->role)
62
            ],
63
            'level' => [
64
                'required',
65
                'int',
66
                Rule::unique('roles')->ignore($this->role)
67
            ],
68
            'color' => 'required|min:7|max:7|hex_color',
69
            'description' => 'min:10',
70
            'permissions' => 'required'
71
        ];
72
    }
73
74
    /**
75
     * Function to store the model.
76
     *
77
     * @return Role
78
     */
79
    public function create(): Role
80
    {
81
        $properties = [
82
            'name',
83
            'color',
84
            'level',
85
            'description'
86
        ];
87
88
        $role = Role::create($this->only($properties));
89
90
        $role->syncPermissions($this->permissions);
91
92
        return $role;
93
    }
94
95
    /**
96
     * Function to update the model.
97
     *
98
     * @return Role
99
     */
100
    public function update(): Role
101
    {
102
        $this->role->update($this->only([
0 ignored issues
show
Bug introduced by
The method update() does not exist on null. ( Ignorable by Annotation )

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

102
        $this->role->/** @scrutinizer ignore-call */ 
103
                     update($this->only([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
            'name',
104
            'color',
105
            'level',
106
            'description'
107
        ]));
108
        $this->role->syncPermissions($this->permissions);
109
        $this->role->fresh();
110
111
        return $this->role;
112
    }
113
}
114