Completed
Pull Request — master (#21)
by Fèvre
06:31 queued 03:41
created

RoleController::delete()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 7
nop 1
1
<?php
2
namespace Xetaravel\Http\Controllers\Admin\Role;
3
4
use Illuminate\Http\RedirectResponse;
5
use Illuminate\Http\Request;
6
use Illuminate\View\View;
7
use Ultraware\Roles\Models\Permission;
8
use Xetaravel\Http\Controllers\Admin\Controller;
9
use Xetaravel\Models\Repositories\RoleRepository;
10
use Xetaravel\Models\Role;
11
use Xetaravel\Models\Validators\RoleValidator;
12
13
class RoleController extends Controller
14
{
15
    /**
16
     * Show the search page.
17
     *
18
     * @return \Illuminate\View\View
19
     */
20 View Code Duplication
    public function index(): View
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22
        $roles = Role::paginate(10);
23
24
        $breadcrumbs = $this->breadcrumbs->addCrumb('Manage Roles', route('admin.role.role.index'));
0 ignored issues
show
Bug introduced by
The property breadcrumbs does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
26
        return view('Admin::Role.role.index', compact('roles', 'breadcrumbs'));
27
    }
28
29
    /**
30
     * Show the role create form.
31
     *
32
     * @return \Illuminate\View\View
33
     */
34 View Code Duplication
    public function showCreateForm(): View
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        $permissions = Permission::pluck('name', 'id');
37
38
        $optionsAttributes = $this->getOptionAttributes();
39
40
        $breadcrumbs = $this->breadcrumbs
41
            ->addCrumb('Manage Roles', route('admin.role.role.index'))
42
            ->addCrumb("Create", route('admin.role.role.create'));
43
44
        return view('Admin::Role.role.create', compact('permissions', 'breadcrumbs', 'optionsAttributes'));
45
    }
46
47
    /**
48
     * Handle a role create request for the application.
49
     *
50
     * @param \Illuminate\Http\Request $request
51
     *
52
     * @return \Illuminate\Http\RedirectResponse
53
     */
54
    public function create(Request $request): RedirectResponse
55
    {
56
        RoleValidator::create($request->all())->validate();
57
58
        $role = RoleRepository::create($request->all());
59
        $role->syncPermissions($request->get('permissions'));
60
61
        return redirect()
62
            ->route('admin.role.role.index')
63
            ->with('success', 'This role has been created successfully !');
64
    }
65
66
    /**
67
     * Show the update form.
68
     *
69
     * @param \Illuminate\Http\Request $request
70
     * @param string $slug The slug of the role.
71
     * @param int $id The id of the role.
72
     *
73
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
0 ignored issues
show
Documentation introduced by
Should the return type not be View|\Illuminate\Contracts\View\Factory?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
74
     */
75
    public function showUpdateForm(Request $request, string $slug, int $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $slug is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
    {
77
        $role = Role::findOrFail($id);
78
79
        $permissions = Permission::pluck('name', 'id');
80
        $permission = Permission::where('slug', 'access.administration')->first();
81
82
        $optionsAttributes = $this->getOptionAttributes();
83
84
        $breadcrumbs = $this->breadcrumbs
85
            ->setCssClasses('breadcrumb breadcrumb-inverse bg-inverse mb-0')
86
            ->addCrumb('Manage Roles', route('admin.role.role.index'))
87
            ->addCrumb(
88
                'Update ' . e($role->name),
89
                route('admin.role.role.update', $role->slug, $role->id)
90
            );
91
92
        return view(
93
            'Admin::Role.role.update',
94
            compact('role', 'permissions', 'breadcrumbs', 'optionsAttributes', 'permission')
95
        );
96
    }
97
98
    /**
99
     * Handle an user update request for the application.
100
     *
101
     * @param \Illuminate\Http\Request $request
102
     * @param int $id The id of the role to update.
103
     *
104
     * @return \Illuminate\Http\RedirectResponse
105
     */
106
    public function update(Request $request, int $id): RedirectResponse
107
    {
108
        $role = Role::findOrFail($id);
109
110
        RoleValidator::update($request->all(), $role->id)->validate();
111
        RoleRepository::update($request->all(), $role);
112
113
        $role->syncPermissions($request->get('permissions'));
114
115
        return redirect()
116
            ->back()
117
            ->with('success', 'This role has been updated successfully !');
118
    }
119
120
    /**
121
     * Handle the delete request for the role.
122
     *
123
     * @param int $id The id of the role to delete.
124
     *
125
     * @return \Illuminate\Http\RedirectResponse
126
     */
127
    public function delete(int $id): RedirectResponse
128
    {
129
        $role = Role::findOrFail($id);
130
131
        if (!$role->is_deletable) {
132
            return redirect()
133
                ->route('admin.role.role.index')
134
                ->with('danger', 'You can not delete this role !');
135
        }
136
137
        // Sync the `user` role on all users for this group.
138
        foreach ($role->users as $user) {
139
            // Only do that if the user does not have another role.
140
            if ($user->roles->count() == 1) {
141
                $user->roles()->sync(Role::find(3), false);
142
            }
143
        }
144
145
        if ($role->delete()) {
146
            return redirect()
147
                ->route('admin.role.role.index')
148
                ->with('success', 'This role has been deleted successfully !');
149
        }
150
151
        return redirect()
152
            ->route('admin.role.role.index')
153
            ->with('danger', 'An error occurred while deleting this role !');
154
    }
155
156
    /**
157
     * Return a list of attributes for the permissions option field.
158
     *
159
     * @return array
160
     */
161
    protected function getOptionAttributes(): array
162
    {
163
        $attributes = Permission::pluck('id')->toArray();
164
        $optionsAttributes = [];
165
166
        foreach ($attributes as $attribute) {
167
            $optionsAttributes[$attribute] = [
168
                'title' => 'Role Information',
169
                'data-content' => Permission::where('id', $attribute)->select('description')->first()->description,
170
                'data-toggle' => 'popover',
171
                'data-trigger' => 'hover',
172
                'data-placement' => 'top'
173
            ];
174
        }
175
176
        return $optionsAttributes;
177
    }
178
}
179