Completed
Push — master ( f9e78a...edb43e )
by Abdelrahman
09:22
created

RolesController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Http\Controllers\Backend;
17
18
use Illuminate\Http\Request;
19
use Rinvex\Fort\Models\Role;
20
use Rinvex\Fort\Models\Ability;
21
use Rinvex\Fort\Http\Controllers\AuthorizedController;
22
23
class RolesController extends AuthorizedController
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected $resource = 'roles';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    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...
34
35
    /**
36
     * Display a listing of the resource.
37
     *
38
     * @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...
39
     */
40
    public function index()
41
    {
42
        $roles = Role::paginate(config('rinvex.fort.backend.items_per_page'));
43
44
        return view('rinvex/fort::backend/roles.index', compact('roles'));
45
    }
46
47
    /**
48
     * Show the form for creating a new resource.
49
     *
50
     * @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...
51
     */
52
    public function create()
53
    {
54
        return $this->form('create', 'store', new Role());
55
    }
56
57
    /**
58
     * Show the form for editing the given resource.
59
     *
60
     * @param \Rinvex\Fort\Models\Role $role
61
     *
62
     * @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...
63
     */
64
    public function edit(Role $role)
65
    {
66
        return $this->form('edit', 'update', $role);
67
    }
68
69
    /**
70
     * Store a newly created resource in storage.
71
     *
72
     * @param \Illuminate\Http\Request $request
73
     *
74
     * @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...
75
     */
76
    public function store(Request $request)
77
    {
78
        return $this->process($request, new Role());
79
    }
80
81
    /**
82
     * Update the given resource in storage.
83
     *
84
     * @param \Illuminate\Http\Request $request
85
     * @param \Rinvex\Fort\Models\Role $role
86
     *
87
     * @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...
88
     */
89
    public function update(Request $request, Role $role)
90
    {
91
        return $this->process($request, $role);
92
    }
93
94
    /**
95
     * Delete the given resource from storage.
96
     *
97
     * @param \Rinvex\Fort\Models\Role $role
98
     *
99
     * @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...
100
     */
101
    public function delete(Role $role)
102
    {
103
        $role->delete();
104
105
        return intend([
106
            'route' => 'rinvex.fort.backend.roles.index',
107
            'with'  => ['warning' => trans('rinvex/fort::messages.role.deleted', ['roleId' => $role->id])],
108
        ]);
109
    }
110
111
    /**
112
     * Show the form for create/update of the given resource.
113
     *
114
     * @param string                   $mode
115
     * @param string                   $action
116
     * @param \Rinvex\Fort\Models\Role $role
117
     *
118
     * @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...
119
     */
120
    protected function form($mode, $action, Role $role)
121
    {
122
        $abilityList = Ability::all()->groupBy('resource')->map(function ($ability) {
123
            return $ability->pluck('name', 'id');
124
        })->toArray();
125
126
        return view('rinvex/fort::backend/roles.form', compact('role', 'abilityList', 'mode', 'action'));
127
    }
128
129
    /**
130
     * Process the form for store/update of the given resource.
131
     *
132
     * @param \Illuminate\Http\Request $request
133
     * @param \Rinvex\Fort\Models\Role $role
134
     *
135
     * @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...
136
     */
137
    protected function process(Request $request, Role $role)
138
    {
139
        // Prepare required input fields
140
        $input = $request->all();
141
142
        // Save role
143
        ! $role->exists ? $role = $role->create($input) : $role->update($input);
144
145
        // Sync abilities
146
        if ($request->user($this->getGuard())->can('grant-abilities')) {
147
            $role->abilities()->sync((array) array_pull($input, 'abilityList'));
148
        }
149
150
        return intend([
151
            'route' => 'rinvex.fort.backend.roles.index',
152
            'with'  => ['success' => trans('rinvex/fort::messages.role.saved', ['roleId' => $role->id])],
153
        ]);
154
    }
155
}
156