Completed
Push — develop ( 3c0bec...e16864 )
by Abdelrahman
01:47
created

RolesController::form()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Http\Controllers\Backend;
6
7
use Illuminate\Http\Request;
8
use Rinvex\Fort\Models\Role;
9
use Rinvex\Fort\Models\Ability;
10
use Cortex\Foundation\Http\Controllers\AuthorizedController;
11
12
class RolesController extends AuthorizedController
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    protected $resource = 'roles';
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected $resourceActionWhitelist = ['assign'];
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $resourceActionWhitelist exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
23
24
    /**
25
     * Display a listing of the resource.
26
     *
27
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\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...
28
     */
29
    public function index()
30
    {
31
        $roles = Role::paginate(config('rinvex.fort.backend.items_per_page'));
32
33
        return view('cortex/fort::backend.roles.index', compact('roles'));
34
    }
35
36
    /**
37
     * Store a newly created resource in storage.
38
     *
39
     * @param \Illuminate\Http\Request $request
40
     *
41
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

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...
42
     */
43
    public function store(Request $request)
44
    {
45
        return $this->process($request, new Role());
46
    }
47
48
    /**
49
     * Update the given resource in storage.
50
     *
51
     * @param \Illuminate\Http\Request $request
52
     * @param \Rinvex\Fort\Models\Role $role
53
     *
54
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

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...
55
     */
56
    public function update(Request $request, Role $role)
57
    {
58
        return $this->process($request, $role);
59
    }
60
61
    /**
62
     * Delete the given resource from storage.
63
     *
64
     * @param \Rinvex\Fort\Models\Role $role
65
     *
66
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

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...
67
     */
68
    public function delete(Role $role)
69
    {
70
        $role->delete();
71
72
        return intend([
73
            'url' => route('backend.roles.index'),
74
            'with' => ['warning' => trans('cortex/fort::messages.role.deleted', ['roleId' => $role->id])],
75
        ]);
76
    }
77
78
    /**
79
     * Show the form for create/update of the given resource.
80
     *
81
     * @param \Rinvex\Fort\Models\Role $role
82
     *
83
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\View\View|\I...\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...
84
     */
85
    public function form(Role $role)
86
    {
87
        $abilityList = Ability::all()->groupBy('resource')->map(function ($ability) {
88
            return $ability->pluck('name', 'id');
89
        })->toArray();
90
91
        return view('cortex/fort::backend.roles.form', compact('role', 'abilityList'));
92
    }
93
94
    /**
95
     * Process the form for store/update of the given resource.
96
     *
97
     * @param \Illuminate\Http\Request $request
98
     * @param \Rinvex\Fort\Models\Role $role
99
     *
100
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\Redirec...inate\Http\JsonResponse?

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...
101
     */
102
    protected function process(Request $request, Role $role)
103
    {
104
        // Prepare required input fields
105
        $input = $request->all();
106
107
        // Save role
108
        $role->fill($input)->save();
109
110
        // Sync abilities
111
        if ($request->user($this->getGuard())->can('grant-abilities')) {
112
            $role->abilities()->sync((array) array_pull($input, 'abilityList'));
113
        }
114
115
        return intend([
116
            'url' => route('backend.roles.index'),
117
            'with' => ['success' => trans('cortex/fort::messages.role.saved', ['roleId' => $role->id])],
118
        ]);
119
    }
120
}
121