UserRoleController::delete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 11
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Chuckbe\Chuckcms\Controllers;
4
5
use Chuckbe\Chuckcms\Chuck\UserRepository;
6
use Chuckbe\Chuckcms\Models\User;
7
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
8
use Illuminate\Foundation\Bus\DispatchesJobs;
9
use Illuminate\Foundation\Validation\ValidatesRequests;
10
use Illuminate\Http\Request;
11
use Illuminate\Routing\Controller as BaseController;
12
use Spatie\Permission\Models\Permission;
13
use Spatie\Permission\Models\Role;
14
15
class UserRoleController extends BaseController
16
{
17
    use AuthorizesRequests;
18
    use DispatchesJobs;
19
    use ValidatesRequests;
20
21
    private $user;
22
    private $userRepository;
23
24
    /**
25
     * Create a new controller instance.
26
     *
27
     * @return void
28
     */
29
    public function __construct(User $user, UserRepository $userRepository)
30
    {
31
        $this->user = $user;
32
        $this->userRepository = $userRepository;
33
    }
34
35
    /**
36
     * Show the dashboard -> roles.
37
     *
38
     * @return \Illuminate\View\View
39
     */
40
    public function index()
41
    {
42
        $roles = Role::all();
43
44
        return view('chuckcms::backend.users.roles.index', compact('roles'));
45
    }
46
47
    public function create(Request $request)
48
    {
49
        $this->validate(request(), [//@todo create custom Request class for page validation
50
            'role_name'     => 'max:185|required',
51
            'role_redirect' => 'max:255|required',
52
        ]);
53
54
        $role = Role::firstOrCreate(['name' => $request->role_name], ['redirect' => $request->role_redirect]);
55
56
        return redirect()->route('dashboard.users.roles.edit', ['role' => $role->id])->with('notification', 'Rol aangemaakt!');
57
    }
58
59
    /**
60
     * Show the edit user page.
61
     *
62
     * @return \Illuminate\View\View
63
     */
64
    public function edit(Role $role)
65
    {
66
        $permissions = Permission::all();
67
68
        return view('chuckcms::backend.users.roles.edit', compact('role', 'permissions'));
69
    }
70
71
    public function save(Request $request)
72
    {
73
        $this->validate(request(), [//@todo create custom Request class for page validation
74
            'role_name'            => 'max:185|required',
75
            'role_redirect'        => 'max:255|required',
76
            'role_id'              => 'required',
77
            'permissions_name.*'   => 'required',
78
            'permissions_active.*' => 'required',
79
        ]);
80
81
        $role = Role::findById($request->role_id);
82
        $role->name = $request->role_name;
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Spatie\Permission\Models\Role. 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...
83
        $role->redirect = $request->role_redirect;
84
        $role->save();
85
86
        $permissions = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $permissions is dead and can be removed.
Loading history...
87
        $countPermissions = count($request->permissions_name);
88
        for ($i = 0; $i < $countPermissions; $i++) {
89
            if ($request->permissions_active[$i] == 1) {
90
                $role->givePermissionTo($request->permissions_name[$i]);
91
            } else {
92
                $role->revokePermissionTo($request->permissions_name[$i]);
93
            }
94
        }
95
96
        //redirect back
97
        return redirect()->route('dashboard.users.roles')->with('notification', 'Rol gewijzigd!');
98
    }
99
100
    /**
101
     * Delete the role.
102
     *
103
     * @return string $status
104
     */
105
    public function delete(Request $request)
106
    {
107
        $this->validate(request(), [
108
            'role_id' => 'required',
109
        ]);
110
111
        $role = Role::findById($request->get('role_id'));
0 ignored issues
show
Bug introduced by
It seems like $request->get('role_id') can also be of type null; however, parameter $id of Spatie\Permission\Models\Role::findById() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

111
        $role = Role::findById(/** @scrutinizer ignore-type */ $request->get('role_id'));
Loading history...
112
        if ($role->delete()) {
113
            return redirect()->route('dashboard.users.roles')->with('notification', 'Rol verwijderd!');
114
        } else {
115
            return redirect()->route('dashboard.users.roles')->with('whoops', 'Er is iets misgegaan, probeer het later nog eens!');
116
        }
117
    }
118
}
119