RolesController::index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Thinkstudeo\Rakshak\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Thinkstudeo\Rakshak\Ability;
7
use Thinkstudeo\Rakshak\Role;
8
9
class RolesController extends Controller
10
{
11
    /**
12
     * Validation rules.
13
     *
14
     * @param \Illuminate\Http\Request $request
15
     * @return mixed
16
     */
17
    protected function rules($role)
18
    {
19
        return [
20
            'name'        => ['required', 'unique:roles,name,'.$role->id ?: null, 'string', 'min:3', 'max:255'],
21
            'label'       => ['required', 'string'],
22
            'description' => ['required', 'string', 'max:255'],
23
            'active'      => ['nullable', 'boolean'],
24
            'abilities'   => ['nullable', 'array'],
25
        ];
26
    }
27
28
    /**
29
     * Display a listing of the resource.
30
     *
31
     * @return \Illuminate\Http\Response
32
     */
33
    public function index()
34
    {
35
        $roles = Role::all();
36
37
        return view('rakshak::roles.index', compact('roles'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('rakshak::ro...dex', compact('roles')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
38
    }
39
40
    /**
41
     * Show the form for creating a new resource.
42
     *
43
     * @return \Illuminate\Http\Response
44
     */
45
    public function create()
46
    {
47
        $abilities = Ability::whereActive(1)->get();
48
        $role = new Role();
49
50
        return view('rakshak::roles.create', compact('abilities', 'role'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('rakshak::ro...t('abilities', 'role')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
51
    }
52
53
    /**
54
     * Store a newly created resource in storage.
55
     *
56
     * @param  \Illuminate\Http\Request  request
57
     * @return \Illuminate\Http\Response
58
     */
59
    public function store(Request $request)
60
    {
61
        $this->authorize('create', Role::class);
62
63
        $validated = $request->validate($this->rules($request));
64
65
        $role = Role::create(array_except($validated, ['abilities']));
0 ignored issues
show
Deprecated Code introduced by
The function array_except() has been deprecated: Arr::except() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

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

65
        $role = Role::create(/** @scrutinizer ignore-deprecated */ array_except($validated, ['abilities']));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
66
67
        if ($request->filled('abilities')) {
68
            foreach ($request->abilities as $ability) {
69
                $role->addAbility($ability['name']);
70
            }
71
        }
72
73
        if ($request->expectsJson()) {
74
            return response()->json(['message' => "New role {$role->name} has been created.", 'record' => $role], 201);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...record' => $role), 201) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
75
        }
76
77
        return redirect(route('rakshak.roles.index'))
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('r...e.' has been created.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
78
            ->with('status', 'success')
79
            ->with('message', "Role {$role->name} has been created.");
80
    }
81
82
    /**
83
     * Show the form for editing the specified resource.
84
     *
85
     * @param  Thinkstudeo\Rakshak\Role $role
0 ignored issues
show
Bug introduced by
The type Thinkstudeo\Rakshak\Http...hinkstudeo\Rakshak\Role was not found. Did you mean Thinkstudeo\Rakshak\Role? If so, make sure to prefix the type with \.
Loading history...
86
     * @return \Illuminate\Http\Response
87
     */
88
    public function edit(Role $role)
89
    {
90
        $abilities = Ability::whereActive(true)->get();
91
92
        return view('rakshak::roles.edit', compact('role', 'abilities'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('rakshak::ro...t('role', 'abilities')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
93
    }
94
95
    /**
96
     * Update the specified resource in storage.
97
     *
98
     * @param  \Illuminate\Http\Request  $request
99
     * @param  \Thinkstudeo\Rakshak\Role $role
100
     * @return \Illuminate\Http\Response
101
     */
102
    public function update(Request $request, Role $role)
103
    {
104
        $this->authorize('update', $role);
105
106
        $validated = $request->validate($this->rules($role));
107
108
        $role = tap($role)->update(array_except($validated, ['abilities']));
0 ignored issues
show
Deprecated Code introduced by
The function array_except() has been deprecated: Arr::except() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

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

108
        $role = tap($role)->update(/** @scrutinizer ignore-deprecated */ array_except($validated, ['abilities']));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
109
110
        if ($request->filled('abilities')) {
111
            $abilityIds = [];
112
            foreach ($request->abilities as $ability) {
113
                $abilityIds[] = $ability['id'];
114
            }
115
            $role->abilities()->sync($abilityIds);
116
        }
117
118
        if ($request->expectsJson()) {
119
            return response()->json(['message' => "The role {$role->name} has been updated.", 'record' => $role], 200);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...record' => $role), 200) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
120
        }
121
122
        return redirect()
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back(...updated successfully.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
123
            ->back()
124
            ->with('status', 'success')
125
            ->with('message', "Role {$role->name} updated successfully.");
126
    }
127
128
    /**
129
     * Remove the specified resource from storage.
130
     *
131
     * @param  \Thinkstudeo\Rakshak\Role $role
132
     * @return \Illuminate\Http\Response
133
     */
134
    public function destroy(Role $role)
135
    {
136
        $role->delete();
137
138
        if (request()->expectsJson()) {
139
            return response()->json(['message' => "The role {$role->name} has been deleted."], 200);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...s been deleted.'), 200) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
140
        }
141
142
        return redirect(route('rakshak.roles.index'))
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect(route('r...e.' has been deleted.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
143
            ->with('status', 'success')
144
            ->with('message', "Role {$role->name} has been deleted.");
145
    }
146
}
147