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 ( becbd2...d73b78 )
by Cristian
01:17
created

UserCrudController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 187
Duplicated Lines 8.56 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 16
loc 187
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 6 1
B setupListOperation() 0 57 1
A setupCreateOperation() 0 5 1
A setupUpdateOperation() 0 5 1
A store() 8 8 1
A update() 8 8 1
A handlePasswordInput() 0 16 2
A addUserFields() 0 54 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        // Role Filter
56
        $this->crud->addFilter([
57
          'name' => 'role',
58
          'type' => 'dropdown',
59
          'label'=> 'Role'
60
        ], 
61
        config('permission.models.role')::all()->pluck('name', 'id')->toArray(), 
62
        function($value) { // if the filter is active
63
          $this->crud->addClause('whereHas', 'roles', function ($query) use ($value) {
64
            $query->where('role_id', '=', $value);
65
          });
66
        });
67
68
        // Extra Permission Filter
69
        $this->crud->addFilter([
70
          'name'  => 'permissions',
71
          'type'  => 'select2',
72
          'label' => 'Extra Permission'
73
        ], 
74
        config('permission.models.permission')::all()->pluck('name', 'id')->toArray(), 
75
        function ($value) { // if the filter is active
76
          $this->crud->addClause('whereHas', 'permissions', function ($query) use ($value) {
77
            $query->where('permission_id', '=', $value);
78
          });
79
        });
80
    }
81
82
    public function setupCreateOperation()
83
    {
84
        $this->addUserFields();
85
        $this->crud->setValidation(StoreRequest::class);
86
    }
87
88
    public function setupUpdateOperation()
89
    {
90
        $this->addUserFields();
91
        $this->crud->setValidation(UpdateRequest::class);
92
    }
93
94
    /**
95
     * Store a newly created resource in the database.
96
     *
97
     * @return \Illuminate\Http\RedirectResponse
98
     */
99 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...
100
    {
101
        $this->crud->request = $this->crud->validateRequest();
102
        $this->crud->request = $this->handlePasswordInput($this->crud->request);
103
        $this->crud->unsetValidation(); // validation has already been run
104
105
        return $this->traitStore();
106
    }
107
108
    /**
109
     * Update the specified resource in the database.
110
     *
111
     * @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...
112
     */
113 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...
114
    {
115
        $this->crud->request = $this->crud->validateRequest();
116
        $this->crud->request = $this->handlePasswordInput($this->crud->request);
117
        $this->crud->unsetValidation(); // validation has already been run
118
119
        return $this->traitUpdate();
120
    }
121
122
    /**
123
     * Handle password input fields.
124
     */
125
    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...
126
    {
127
        // Remove fields not present on the user.
128
        $request->request->remove('password_confirmation');
129
        $request->request->remove('roles_show');
130
        $request->request->remove('permissions_show');
131
132
        // Encrypt password if specified.
133
        if ($request->input('password')) {
134
            $request->request->set('password', Hash::make($request->input('password')));
135
        } else {
136
            $request->request->remove('password');
137
        }
138
139
        return $request;
140
    }
141
142
    protected function addUserFields()
143
    {
144
        $this->crud->addFields([
145
            [
146
                'name'  => 'name',
147
                'label' => trans('backpack::permissionmanager.name'),
148
                'type'  => 'text',
149
            ],
150
            [
151
                'name'  => 'email',
152
                'label' => trans('backpack::permissionmanager.email'),
153
                'type'  => 'email',
154
            ],
155
            [
156
                'name'  => 'password',
157
                'label' => trans('backpack::permissionmanager.password'),
158
                'type'  => 'password',
159
            ],
160
            [
161
                'name'  => 'password_confirmation',
162
                'label' => trans('backpack::permissionmanager.password_confirmation'),
163
                'type'  => 'password',
164
            ],
165
            [
166
            // two interconnected entities
167
            'label'             => trans('backpack::permissionmanager.user_role_permission'),
168
            'field_unique_name' => 'user_role_permission',
169
            'type'              => 'checklist_dependency',
170
            'name'              => ['roles', 'permissions'],
171
            'subfields'         => [
172
                    'primary' => [
173
                        'label'            => trans('backpack::permissionmanager.roles'),
174
                        'name'             => 'roles', // the method that defines the relationship in your Model
175
                        'entity'           => 'roles', // the method that defines the relationship in your Model
176
                        'entity_secondary' => 'permissions', // the method that defines the relationship in your Model
177
                        'attribute'        => 'name', // foreign key attribute that is shown to user
178
                        'model'            => config('permission.models.role'), // foreign key model
179
                        'pivot'            => true, // on create&update, do you need to add/delete pivot table entries?]
180
                        'number_columns'   => 3, //can be 1,2,3,4,6
181
                    ],
182
                    'secondary' => [
183
                        'label'          => ucfirst(trans('backpack::permissionmanager.permission_singular')),
184
                        'name'           => 'permissions', // the method that defines the relationship in your Model
185
                        'entity'         => 'permissions', // the method that defines the relationship in your Model
186
                        'entity_primary' => 'roles', // the method that defines the relationship in your Model
187
                        'attribute'      => 'name', // foreign key attribute that is shown to user
188
                        'model'          => config('permission.models.permission'), // foreign key model
189
                        'pivot'          => true, // on create&update, do you need to add/delete pivot table entries?]
190
                        'number_columns' => 3, //can be 1,2,3,4,6
191
                    ],
192
                ],
193
            ],
194
        ]);
195
    }
196
}
197