Completed
Pull Request — master (#21)
by Fèvre
04:45 queued 02:13
created

RoleController::showUpdateForm()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 22

Duplication

Lines 9
Ratio 28.13 %

Importance

Changes 0
Metric Value
dl 9
loc 32
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 22
nc 2
nop 3
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 Xetaravel\Models\Role;
8
use Ultraware\Roles\Models\Permission;
9
use Xetaravel\Http\Controllers\Admin\Controller;
10
use Xetaravel\Models\Validators\RoleValidator;
11
use Xetaravel\Models\Repositories\RoleRepository;
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
    public function showCreateForm(): View
35
    {
36
        $permissions = Permission::pluck('name', 'id');
37
        $attributes = Permission::pluck('id')->toArray();
38
39
        $optionsAttributes = [];
40 View Code Duplication
        foreach ($attributes as $attribute) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
41
            $optionsAttributes[$attribute] = [
42
                'title' => 'Role Information',
43
                'data-content' => Permission::where('id', $attribute)->select('description')->first()->description,
44
                'data-toggle' => 'popover',
45
                'data-trigger' => 'hover',
46
                'data-placement' => 'top'
47
            ];
48
        }
49
50
        $breadcrumbs = $this->breadcrumbs
51
            ->addCrumb('Manage Roles', route('admin.role.role.index'))
52
            ->addCrumb("Create", route('admin.role.role.create'));
53
54
        return view('Admin::Role.role.create', compact('permissions', 'breadcrumbs', 'optionsAttributes'));
55
    }
56
57
    /**
58
     * Show the update form.
59
     *
60
     * @param \Illuminate\Http\Request $request
61
     * @param string $slug The slug of the role.
62
     * @param int $id The id of the role.
63
     *
64
     * @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...
65
     */
66
    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...
67
    {
68
        $role = Role::findOrFail($id);
69
70
        $permissions = Permission::pluck('name', 'id');
71
        $attributes = Permission::pluck('id')->toArray();
72
        $permission = Permission::where('slug', 'access.administration')->first();
73
74
        $optionsAttributes = [];
75 View Code Duplication
        foreach ($attributes as $attribute) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
76
            $optionsAttributes[$attribute] = [
77
                'title' => 'Role Information',
78
                'data-content' => Permission::where('id', $attribute)->select('description')->first()->description,
79
                'data-toggle' => 'popover',
80
                'data-trigger' => 'hover',
81
                'data-placement' => 'top'
82
            ];
83
        }
84
85
        $breadcrumbs = $this->breadcrumbs
86
            ->setCssClasses('breadcrumb breadcrumb-inverse bg-inverse mb-0')
87
            ->addCrumb('Manage Roles', route('admin.role.role.index'))
88
            ->addCrumb(
89
                'Update ' . e($role->name),
90
                route('admin.role.role.update', $role->slug, $role->id)
91
            );
92
93
        return view(
94
            'Admin::Role.role.update',
95
            compact('role', 'permissions', 'breadcrumbs', 'optionsAttributes', 'permission')
96
        );
97
    }
98
99
    /**
100
     * Handle an user update request for the application.
101
     *
102
     * @param \Illuminate\Http\Request $request
103
     * @param int $id The id of the role to update.
104
     *
105
     * @return \Illuminate\Http\RedirectResponse
106
     */
107
    public function update(Request $request, int $id): RedirectResponse
108
    {
109
        $role = Role::findOrFail($id);
110
111
        RoleValidator::update($request->all(), $role->id)->validate();
112
        RoleRepository::update($request->all(), $role);
113
114
        $role->syncPermissions($request->get('permissions'));
115
116
        return redirect()
117
            ->back()
118
            ->with('success', 'This role has been updated successfully !');
119
    }
120
}
121