Passed
Branch dev (9fd620)
by Darko
16:00
created

RoleController::edit()   F

Complexity

Conditions 49
Paths > 20000

Size

Total Lines 122
Code Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 49
eloc 81
nc 354302
nop 1
dl 0
loc 122
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use Illuminate\Http\Request;
6
use Spatie\Permission\Models\Role;
7
use App\Http\Controllers\BasePageController;
8
9
class RoleController extends BasePageController
10
{
11
    /**
12
     * @throws \Exception
13
     */
14
    public function index()
15
    {
16
        $this->setAdminPrefs();
17
18
        $title = 'User Role List';
19
20
        //get the user roles
21
        $userroles = Role::all();
22
23
        $this->smarty->assign('userroles', $userroles);
0 ignored issues
show
Bug introduced by
The method assign() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

23
        $this->smarty->/** @scrutinizer ignore-call */ 
24
                       assign('userroles', $userroles);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
25
        $content = $this->smarty->fetch('role-list.tpl');
0 ignored issues
show
Bug introduced by
The method fetch() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

25
        /** @scrutinizer ignore-call */ 
26
        $content = $this->smarty->fetch('role-list.tpl');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
26
27
        $this->smarty->assign([
28
                'title' => $title,
29
                'meta_title' => $title,
30
                'content' => $content,
31
            ]);
32
33
        $this->adminrender();
34
    }
35
36
    /**
37
     * @param \Illuminate\Http\Request $request
38
     *
39
     * @throws \Exception
40
     */
41
    public function create(Request $request): void
42
    {
43
        $this->setAdminPrefs();
44
45
        switch ($request->input('action') ?? 'view') {
46
            case 'submit':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
47
                $title = 'Add User Role';
48
                    $role = Role::create([
49
                        'name' => $request->input('name'),
50
                        'apirequests' => $request->input('apirequests'),
51
                        'downloadrequests' => $request->input('downloadrequests'),
52
                        'defaultinvites' => $request->input('defaultinvites'),
53
                        'donation' => $request->input('donation') ?? 0,
54
                        'addyears' => $request->input('addyears') ?? 0,
55
                        'rate_limit' => $request->input('rate_limit'),
56
                    ]);
57
                    if ((int) $request->input('canpreview') === 1) {
58
                        $role->givePermissionTo('preview');
59
                    }
60
61
                    if ((int) $request->input('hideads') === 1) {
62
                        $role->givePermissionTo('hideads');
63
                    }
64
65
                    if ((int) $request->input('editrelease') === 1) {
66
                        $role->givePermissionTo('edit release');
67
                    }
68
69
                    if ((int) $request->input('viewconsole') === 1) {
70
                        $role->givePermissionTo('view console');
71
                    }
72
73
                    if ((int) $request->input('viewmovies') === 1) {
74
                        $role->givePermissionTo('view movies');
75
                    }
76
77
                    if ((int) $request->input('viewaudio') === 1) {
78
                        $role->givePermissionTo('view audio');
79
                    }
80
81
                    if ((int) $request->input('viewpc') === 1) {
82
                        $role->givePermissionTo('view pc');
83
                    }
84
85
                    if ((int) $request->input('viewtv') === 1) {
86
                        $role->givePermissionTo('view tv');
87
                    }
88
89
                    if ((int) $request->input('viewadult') === 1) {
90
                        $role->givePermissionTo('view adult');
91
                    }
92
93
                    if ((int) $request->input('viewbooks') === 1) {
94
                        $role->givePermissionTo('view books');
95
                    }
96
97
                    if ((int) $request->input('viewother') === 1) {
98
                        $role->givePermissionTo('view other');
99
                    }
100
                redirect()->to('admin/role-list')->sendHeaders();
101
                break;
102
            case 'view':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
103
            default:
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
104
            $title = 'Add User Role';
105
            $role = [
106
            ];
107
108
            break;
109
        }
110
111
        $this->smarty->assign('yesno_ids', [1, 0]);
112
        $this->smarty->assign('yesno_names', ['Yes', 'No']);
113
114
        $content = $this->smarty->fetch('role-add.tpl');
115
116
        $this->smarty->assign(
117
            [
118
                'title' => $title,
119
                'meta_title' => $title,
120
                'content' => $content,
121
                'role' => $role,
122
            ]
123
        );
124
125
        $this->adminrender();
126
    }
127
128
    /**
129
     * @param \Illuminate\Http\Request $request
130
     *
131
     * @throws \Exception
132
     */
133
    public function edit(Request $request): void
134
    {
135
        $this->setAdminPrefs();
136
137
        $title = 'User Roles';
138
139
        // Get the user roles.
140
        $userRoles = Role::all();
141
        $roles = [];
142
        foreach ($userRoles as $userRole) {
143
            $roles[$userRole['id']] = $userRole['name'];
144
        }
145
146
        switch ($request->input('action') ?? 'view') {
147
            case 'submit':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
148
                    $title = 'Update User Role';
149
                    $role = Role::find($request->input('id'));
150
                    $role->update([
151
                        'name' => $request->input('name'),
152
                        'apirequests' => $request->input('apirequests'),
153
                        'downloadrequests' => $request->input('downloadrequests'),
154
                        'defaultinvites' => $request->input('defaultinvites'),
155
                        'isdefault' => $request->input('isdefault'),
156
                        'donation' => $request->input('donation'),
157
                        'addyears' => $request->input('addyears'),
158
                        'rate_limit' => $request->input('rate_limit'),
159
                    ]);
160
161
                    if ((int) $request->input('canpreview') === 1 && $role->hasPermissionTo('preview') === false) {
162
                        $role->givePermissionTo('preview');
163
                    } elseif ((int) $request->input('canpreview') === 0 && $role->hasPermissionTo('preview') === true) {
164
                        $role->revokePermissionTo('preview');
165
                    }
166
167
                    if ((int) $request->input('hideads') === 1 && $role->hasPermissionTo('hideads') === false) {
168
                        $role->givePermissionTo('hideads');
169
                    } elseif ((int) $request->input('hideads') === 0 && $role->hasPermissionTo('hideads') === true) {
170
                        $role->revokePermissionTo('hideads');
171
                    }
172
173
                    if ((int) $request->input('editrelease') === 1 && $role->hasPermissionTo('edit release') === false) {
174
                        $role->givePermissionTo('edit release');
175
                    } elseif ((int) $request->input('editrelease') === 0 && $role->hasPermissionTo('edit release') === true) {
176
                        $role->revokePermissionTo('edit release');
177
                    }
178
179
                    if ((int) $request->input('viewconsole') === 1 && $role->hasPermissionTo('view console') === false) {
180
                        $role->givePermissionTo('view console');
181
                    } elseif ((int) $request->input('viewconsole') === 0 && $role->hasPermissionTo('view console') === true) {
182
                        $role->revokePermissionTo('view console');
183
                    }
184
185
                    if ((int) $request->input('viewmovies') === 1 && $role->hasPermissionTo('view movies') === false) {
186
                        $role->givePermissionTo('view movies');
187
                    } elseif ((int) $request->input('viewmovies') === 0 && $role->hasPermissionTo('view movies') === true) {
188
                        $role->revokePermissionTo('view movies');
189
                    }
190
191
                    if ((int) $request->input('viewaudio') === 1 && $role->hasPermissionTo('view audio') === false) {
192
                        $role->givePermissionTo('view audio');
193
                    } elseif ((int) $request->input('viewaudio') === 0 && $role->hasPermissionTo('view audio') === true) {
194
                        $role->revokePermissionTo('view audio');
195
                    }
196
197
                    if ((int) $request->input('viewpc') === 1 && $role->hasPermissionTo('view pc') === false) {
198
                        $role->givePermissionTo('view pc');
199
                    } elseif ((int) $request->input('viewpc') === 0 && $role->hasPermissionTo('view pc') === true) {
200
                        $role->revokePermissionTo('view pc');
201
                    }
202
203
                    if ((int) $request->input('viewtv') === 1 && $role->hasPermissionTo('view tv') === false) {
204
                        $role->givePermissionTo('view tv');
205
                    } elseif ((int) $request->input('viewtv') === 0 && $role->hasPermissionTo('view tv') === true) {
206
                        $role->revokePermissionTo('view tv');
207
                    }
208
209
                    if ((int) $request->input('viewadult') === 1 && $role->hasPermissionTo('view adult') === false) {
210
                        $role->givePermissionTo('view adult');
211
                    } elseif ((int) $request->input('viewadult') === 0 && $role->hasPermissionTo('view adult') === true) {
212
                        $role->revokePermissionTo('view adult');
213
                    }
214
215
                    if ((int) $request->input('viewbooks') === 1 && $role->hasPermissionTo('view books') === false) {
216
                        $role->givePermissionTo('view books');
217
                    } elseif ((int) $request->input('viewbooks') === 0 && $role->hasPermissionTo('view books') === true) {
218
                        $role->revokePermissionTo('view books');
219
                    }
220
221
                    if ((int) $request->input('viewother') === 1 && $role->hasPermissionTo('view other') === false) {
222
                        $role->givePermissionTo('view other');
223
                    } elseif ((int) $request->input('viewother') === 0 && $role->hasPermissionTo('view other') === true) {
224
                        $role->revokePermissionTo('view other');
225
                    }
226
227
                $this->smarty->assign('role', $role);
228
                redirect()->to('admin/role-list')->sendHeaders();
229
                break;
230
231
            case 'view':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
232
            default:
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
233
                if ($request->has('id')) {
234
                    $title = 'User Roles Edit';
235
                    $role = Role::findById($request->input('id'));
236
                    $this->smarty->assign('role', $role);
237
                }
238
                break;
239
        }
240
241
        $this->smarty->assign('yesno_ids', [1, 0]);
242
        $this->smarty->assign('yesno_names', ['Yes', 'No']);
243
244
        $content = $this->smarty->fetch('role-edit.tpl');
245
246
        $this->smarty->assign(
247
            [
248
                'title' => $title,
249
                'meta_title' => $title,
250
                'content' => $content,
251
            ]
252
        );
253
254
        $this->adminrender();
255
    }
256
257
    /**
258
     * @param \Illuminate\Http\Request $request
259
     *
260
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
261
     */
262
    public function destroy(Request $request)
263
    {
264
        if ($request->has('id')) {
265
            Role::query()->where('id', $request->input('id'))->delete();
266
        }
267
268
        return redirect($request->server('HTTP_REFERER'));
269
    }
270
}
271