Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — master ( 766727...7a5d19 )
by Cristian
08:55
created

UserCrudController::setup()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 89
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 89
rs 8.5731
c 1
b 0
f 0
cc 1
eloc 61
nc 1
nop 0

How to fix   Long Method   

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 Backpack\PermissionManager\app\Http\Controllers;
4
5
use App\Http\Requests;
6
use Backpack\CRUD\app\Http\Controllers\CrudController;
7
use Backpack\PermissionManager\app\Http\Requests\UserStoreCrudRequest as StoreRequest;
8
// VALIDATION
9
use Backpack\PermissionManager\app\Http\Requests\UserUpdateCrudRequest as UpdateRequest;
10
use Illuminate\Http\Request;
11
12
class UserCrudController extends CrudController
13
{
14
    public function setup()
15
    {
16
        $this->crud->setModel(config('backpack.permissionmanager.user_model'));
17
        $this->crud->setEntityNameStrings(trans('backpack::permissionmanager.user'), trans('backpack::permissionmanager.users'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
18
        $this->crud->setRoute(config('backpack.base.route_prefix').'/user');
19
20
        $this->crud->setColumns([
21
            [
22
                'name'  => 'name',
23
                'label' => trans('backpack::permissionmanager.name'),
24
                'type'  => 'text',
25
            ],
26
            [
27
                'name'  => 'email',
28
                'label' => trans('backpack::permissionmanager.email'),
29
                'type'  => 'email',
30
            ],
31
        ]);
32
33
        $this->crud->addColumn([ // n-n relationship (with pivot table)
34
           'label'     => trans('backpack::permissionmanager.roles'), // Table column heading
35
           'type'      => 'select_multiple',
36
           'name'      => 'roles', // the method that defines the relationship in your Model
37
           'entity'    => 'roles', // the method that defines the relationship in your Model
38
           'attribute' => 'name', // foreign key attribute that is shown to user
39
           'model'     => "Backpack\PermissionManager\app\Models\Roles", // foreign key model
40
        ]);
41
42
        $this->crud->addColumn([ // n-n relationship (with pivot table)
43
           'label'     => trans('backpack::permissionmanager.extra_permissions'), // Table column heading
44
           'type'      => 'select_multiple',
45
           'name'      => 'permissions', // the method that defines the relationship in your Model
46
           'entity'    => 'permissions', // the method that defines the relationship in your Model
47
           'attribute' => 'name', // foreign key attribute that is shown to user
48
           'model'     => "Backpack\PermissionManager\app\Models\Permission", // foreign key model
49
        ]);
50
51
        $this->crud->addFields([
52
            [
53
                'name'  => 'name',
54
                'label' => trans('backpack::permissionmanager.name'),
55
                'type'  => 'text',
56
            ],
57
            [
58
                'name'  => 'email',
59
                'label' => trans('backpack::permissionmanager.email'),
60
                'type'  => 'email',
61
            ],
62
            [
63
                'name'  => 'password',
64
                'label' => trans('backpack::permissionmanager.password'),
65
                'type'  => 'password',
66
            ],
67
            [
68
                'name'  => 'password_confirmation',
69
                'label' => trans('backpack::permissionmanager.password_confirmation'),
70
                'type'  => 'password',
71
            ],
72
            [
73
            // two interconnected entities
74
            'label'             => trans('backpack::permissionmanager.user_role_permission'),
75
            'field_unique_name' => 'user_role_permission',
76
            'type'              => 'checklist_dependency',
77
            'name'              => 'roles_and_permissions', // the methods that defines the relationship in your Model
78
            'subfields'         => [
79
                    'primary' => [
80
                        'label'            => trans('backpack::permissionmanager.roles'),
81
                        'name'             => 'roles', // the method that defines the relationship in your Model
82
                        'entity'           => 'roles', // the method that defines the relationship in your Model
83
                        'entity_secondary' => 'permissions', // the method that defines the relationship in your Model
84
                        'attribute'        => 'name', // foreign key attribute that is shown to user
85
                        'model'            => "Backpack\PermissionManager\app\Models\Role", // foreign key model
86
                        'pivot'            => true, // on create&update, do you need to add/delete pivot table entries?]
87
                        'number_columns'   => 3, //can be 1,2,3,4,6
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
88
                    ],
89
                    'secondary' => [
90
                        'label'          => ucfirst(trans('backpack::permissionmanager.permission_singular')),
91
                        'name'           => 'permissions', // the method that defines the relationship in your Model
92
                        'entity'         => 'permissions', // the method that defines the relationship in your Model
93
                        'entity_primary' => 'roles', // the method that defines the relationship in your Model
94
                        'attribute'      => 'name', // foreign key attribute that is shown to user
95
                        'model'          => "Backpack\PermissionManager\app\Models\Permission", // foreign key model
96
                        'pivot'          => true, // on create&update, do you need to add/delete pivot table entries?]
97
                        'number_columns' => 3, //can be 1,2,3,4,6
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
                    ],
99
                ],
100
            ],
101
        ]);
102
    }
103
104
    /**
105
     * Store a newly created resource in the database.
106
     *
107
     * @param StoreRequest $request - type injection used for validation using Requests
108
     *
109
     * @return \Illuminate\Http\RedirectResponse
110
     */
111
    public function store(StoreRequest $request)
112
    {
113
        $this->crud->hasAccessOrFail('create');
114
115
        // insert item in the db
116
        if ($request->input('password')) {
117
            $item = $this->crud->create(\Request::except(['redirect_after_save']));
118
119
            // now bcrypt the password
120
            $item->password = bcrypt($request->input('password'));
0 ignored issues
show
Bug introduced by
It seems like $request->input('password') targeting Illuminate\Http\Request::input() can also be of type array; however, bcrypt() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
121
            $item->save();
122
        } else {
123
            $item = $this->crud->create(\Request::except(['redirect_after_save', 'password']));
124
        }
125
126
        // show a success message
127
        \Alert::success(trans('backpack::crud.insert_success'))->flash();
128
129
        // redirect the user where he chose to be redirected
130
        switch (\Request::input('redirect_after_save')) {
131
            case 'current_item_edit':
132
                return \Redirect::to($this->crud->route.'/'.$item->id.'/edit');
133
134
            default:
135
                return \Redirect::to(\Request::input('redirect_after_save'));
0 ignored issues
show
Bug introduced by
It seems like \Request::input('redirect_after_save') targeting Illuminate\Http\Request::input() can also be of type array; however, Illuminate\Routing\Redirector::to() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
136
        }
137
    }
138
139
    public function update(UpdateRequest $request)
140
    {
141
        //encrypt password and set it to request
142
        $this->crud->hasAccessOrFail('update');
143
144
        $dataToUpdate = \Request::except(['redirect_after_save', 'password']);
145
146
        //encrypt password
147
        if ($request->input('password')) {
148
            $dataToUpdate['password'] = bcrypt($request->input('password'));
0 ignored issues
show
Bug introduced by
It seems like $request->input('password') targeting Illuminate\Http\Request::input() can also be of type array; however, bcrypt() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
149
        }
150
151
        // update the row in the db
152
        $this->crud->update(\Request::get('id'), $dataToUpdate);
153
154
        // show a success message
155
        \Alert::success(trans('backpack::crud.update_success'))->flash();
156
157
        return \Redirect::to($this->crud->route);
158
    }
159
}
160