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
02:32 queued 01:18
created

UserCrudController::setupUpdateOperation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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'));
0 ignored issues
show
Bug introduced by
It seems like backpack_url('user') targeting backpack_url() can also be of type object<Illuminate\Contracts\Routing\UrlGenerator>; however, Backpack\CRUD\CrudPanel::setRoute() 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...
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();
0 ignored issues
show
Documentation Bug introduced by
The method traitStore does not exist on object<Backpack\Permissi...ers\UserCrudController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
80
    }
81
82
    /**
83
     * Update the specified resource in the database.
84
     *
85
     * @return \Illuminate\Http\RedirectResponse
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();
0 ignored issues
show
Bug introduced by
The method traitUpdate() does not exist on Backpack\PermissionManag...lers\UserCrudController. Did you maybe mean update()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
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