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
Pull Request — master (#199)
by Cristian
01:09
created

UserCrudController::addUserFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 9.0036
c 0
b 0
f 0
cc 1
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 Backpack\CRUD\app\Http\Controllers\CrudController;
6
use Backpack\PermissionManager\app\Http\Requests\UserStoreCrudRequest as StoreRequest;
7
use Backpack\PermissionManager\app\Http\Requests\UserUpdateCrudRequest as UpdateRequest;
8
use Illuminate\Support\Facades\Hash;
9
10
class UserCrudController extends CrudController
11
{
12
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
13
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { store as traitStore; }
14
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { update as traitUpdate; }
15
    use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
16
17
    public function setup()
18
    {
19
        $this->crud->setModel(config('backpack.permissionmanager.models.user'));
20
        $this->crud->setEntityNameStrings(trans('backpack::permissionmanager.user'), trans('backpack::permissionmanager.users'));
21
        $this->crud->setRoute(backpack_url('user'));
22
    }
23
24
    public function setupListOperation()
25
    {
26
        $this->crud->setColumns([
27
            [
28
                'name'  => 'name',
29
                'label' => trans('backpack::permissionmanager.name'),
30
                'type'  => 'text',
31
            ],
32
            [
33
                'name'  => 'email',
34
                'label' => trans('backpack::permissionmanager.email'),
35
                'type'  => 'email',
36
            ],
37
            [ // n-n relationship (with pivot table)
38
               'label'     => trans('backpack::permissionmanager.roles'), // Table column heading
39
               'type'      => 'select_multiple',
40
               'name'      => 'roles', // the method that defines the relationship in your Model
41
               'entity'    => 'roles', // the method that defines the relationship in your Model
42
               'attribute' => 'name', // foreign key attribute that is shown to user
43
               'model'     => config('permission.models.role'), // foreign key model
44
            ],
45
            [ // n-n relationship (with pivot table)
46
               'label'     => trans('backpack::permissionmanager.extra_permissions'), // Table column heading
47
               'type'      => 'select_multiple',
48
               'name'      => 'permissions', // the method that defines the relationship in your Model
49
               'entity'    => 'permissions', // the method that defines the relationship in your Model
50
               'attribute' => 'name', // foreign key attribute that is shown to user
51
               'model'     => config('permission.models.permission'), // foreign key model
52
            ],
53
        ]);
54
    }
55
56
    public function setupCreateOperation()
57
    {
58
        $this->addUserFields();
59
        $this->crud->setValidation(StoreRequest::class);
60
    }
61
62
    public function setupUpdateOperation()
63
    {
64
        $this->addUserFields();
65
        $this->crud->setValidation(UpdateRequest::class);
66
    }
67
68
    /**
69
     * Store a newly created resource in the database.
70
     *
71
     * @return \Illuminate\Http\RedirectResponse
72
     */
73 View Code Duplication
    public function store()
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...
74
    {
75
        $this->crud->request = $this->crud->validateRequest();
76
        $this->crud->request = $this->handlePasswordInput($this->crud->request);
77
        $this->crud->unsetValidation(); // validation has already been run
78
79
        return $this->traitStore();
80
    }
81
82
    /**
83
     * Update the specified resource in the database.
84
     *
85
     * @return \Illuminate\Http\RedirectResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Backpack\CRUD\app\Http\...ers\Operations\Response?

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...
86
     */
87 View Code Duplication
    public function update()
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...
88
    {
89
        $this->crud->request = $this->crud->validateRequest();
90
        $this->crud->request = $this->handlePasswordInput($this->crud->request);
91
        $this->crud->unsetValidation(); // validation has already been run
92
93
        return $this->traitUpdate();
94
    }
95
96
    /**
97
     * Handle password input fields.
98
     */
99
    protected function handlePasswordInput($request)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
100
    {
101
        // Remove fields not present on the user.
102
        $request->request->remove('password_confirmation');
103
        $request->request->remove('roles_show');
104
        $request->request->remove('permissions_show');
105
106
        // Encrypt password if specified.
107
        if ($request->input('password')) {
108
            $request->request->set('password', Hash::make($request->input('password')));
109
        } else {
110
            $request->request->remove('password');
111
        }
112
113
        return $request;
114
    }
115
116
    protected function addUserFields()
117
    {
118
        $this->crud->addFields([
119
            [
120
                'name'  => 'name',
121
                'label' => trans('backpack::permissionmanager.name'),
122
                'type'  => 'text',
123
            ],
124
            [
125
                'name'  => 'email',
126
                'label' => trans('backpack::permissionmanager.email'),
127
                'type'  => 'email',
128
            ],
129
            [
130
                'name'  => 'password',
131
                'label' => trans('backpack::permissionmanager.password'),
132
                'type'  => 'password',
133
            ],
134
            [
135
                'name'  => 'password_confirmation',
136
                'label' => trans('backpack::permissionmanager.password_confirmation'),
137
                'type'  => 'password',
138
            ],
139
            [
140
            // two interconnected entities
141
            'label'             => trans('backpack::permissionmanager.user_role_permission'),
142
            'field_unique_name' => 'user_role_permission',
143
            'type'              => 'checklist_dependency',
144
            'name'              => 'roles_and_permissions', // the methods that defines the relationship in your Model
145
            'subfields'         => [
146
                    'primary' => [
147
                        'label'            => trans('backpack::permissionmanager.roles'),
148
                        'name'             => 'roles', // the method that defines the relationship in your Model
149
                        'entity'           => 'roles', // the method that defines the relationship in your Model
150
                        'entity_secondary' => 'permissions', // the method that defines the relationship in your Model
151
                        'attribute'        => 'name', // foreign key attribute that is shown to user
152
                        'model'            => config('permission.models.role'), // foreign key model
153
                        'pivot'            => true, // on create&update, do you need to add/delete pivot table entries?]
154
                        'number_columns'   => 3, //can be 1,2,3,4,6
155
                    ],
156
                    'secondary' => [
157
                        'label'          => ucfirst(trans('backpack::permissionmanager.permission_singular')),
158
                        'name'           => 'permissions', // the method that defines the relationship in your Model
159
                        'entity'         => 'permissions', // the method that defines the relationship in your Model
160
                        'entity_primary' => 'roles', // the method that defines the relationship in your Model
161
                        'attribute'      => 'name', // foreign key attribute that is shown to user
162
                        'model'          => config('permission.models.permission'), // foreign key model
163
                        'pivot'          => true, // on create&update, do you need to add/delete pivot table entries?]
164
                        'number_columns' => 3, //can be 1,2,3,4,6
165
                    ],
166
                ],
167
            ],
168
        ]);
169
    }
170
}
171