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 ( b324ef...3736af )
by Cristian
13:56
created

UserCrudController::store()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 25
rs 8.8571
cc 3
eloc 12
nc 4
nop 1
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 __construct()
15
    {
16
        parent::__construct();
17
18
        $this->crud->setModel("App\User");
19
        $this->crud->setEntityNameStrings('user', 'users');
20
        $this->crud->setRoute('admin/user');
21
        $this->crud->setColumns([
22
                [
23
                    'name'  => 'name',
24
                    'label' => 'Name',
25
                    'type'  => 'text',
26
                ],
27
                [
28
                    'name'  => 'email',
29
                    'label' => 'Email',
30
                    'type'  => 'email',
31
                ],
32
            ]);
33
34
        $this->crud->addColumn([ // n-n relationship (with pivot table)
35
           'label' => "Roles", // Table column heading
36
           'type' => "select_multiple",
37
           'name' => 'roles', // the method that defines the relationship in your Model
38
           'entity' => 'roles', // the method that defines the relationship in your Model
39
           'attribute' => "name", // foreign key attribute that is shown to user
40
           'model' => "Backpack\PermissionManager\app\Models\Roles", // foreign key model
41
        ]);
42
43
        $this->crud->addColumn([ // n-n relationship (with pivot table)
44
           'label' => "Extra Permissions", // Table column heading
45
           'type' => "select_multiple",
46
           'name' => 'permissions', // the method that defines the relationship in your Model
47
           'entity' => 'permissions', // the method that defines the relationship in your Model
48
           'attribute' => "name", // foreign key attribute that is shown to user
49
           'model' => "Backpack\PermissionManager\app\Models\Permission", // foreign key model
50
        ]);
51
52
        $this->crud->addFields([
53
                                [
54
                                    'name'  => 'name',
55
                                    'label' => 'Name',
56
                                    'type'  => 'text',
57
                                ],
58
                                [
59
                                    'name'  => 'email',
60
                                    'label' => 'Email',
61
                                    'type'  => 'email',
62
                                ],
63
                                [
64
                                    'name'  => 'password',
65
                                    'label' => 'Password',
66
                                    'type'  => 'password',
67
                                ],
68
                                [
69
                                    'name'  => 'password_confirmation',
70
                                    'label' => 'Password Confirmation',
71
                                    'type'  => 'password',
72
                                ],
73
                                [
74
                                // two interconnected entities
75
                                'label'             => 'User Role Permissions',
76
                                'field_unique_name' => 'user_role_permission',
77
                                'type'              => 'checklist_dependency',
78
                                'name'              => 'roles_and_permissions', // the methods that defines the relationship in your Model
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 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...
79
                                'subfields'         => [
80
                                        'primary' => [
81
                                            'label'            => 'Roles',
82
                                            'name'             => 'roles', // the method that defines the relationship in your Model
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 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...
83
                                            'entity'           => 'roles', // the method that defines the relationship in your Model
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 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...
84
                                            'entity_secondary' => 'permissions', // the method that defines the relationship in your Model
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 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...
85
                                            'attribute'        => 'name', // foreign key attribute that is shown to user
86
                                            'model'            => "Backpack\PermissionManager\app\Models\Role", // foreign key model
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 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...
87
                                            'pivot'            => true, // on create&update, do you need to add/delete pivot table entries?]
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 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...
88
                                            '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...
89
                                        ],
90
                                        'secondary' => [
91
                                            'label'          => 'Permission',
92
                                            'name'           => 'permissions', // the method that defines the relationship in your Model
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 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...
93
                                            'entity'         => 'permissions', // the method that defines the relationship in your Model
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 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...
94
                                            'entity_primary' => 'roles', // the method that defines the relationship in your Model
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 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...
95
                                            'attribute'      => 'name', // foreign key attribute that is shown to user
96
                                            'model'          => "Backpack\PermissionManager\app\Models\Permission", // foreign key model
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 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...
97
                                            'pivot'          => true, // on create&update, do you need to add/delete pivot table entries?]
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 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...
98
                                            '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...
99
                                        ],
100
                                    ],
101
                                ],
102
                            ]);
103
    }
104
105
    /**
106
     * Store a newly created resource in the database.
107
     *
108
     * @param StoreRequest $request - type injection used for validation using Requests
109
     *
110
     * @return \Illuminate\Http\RedirectResponse
111
     */
112
    public function store(StoreRequest $request)
113
    {
114
        $this->crud->hasAccessOrFail('create');
115
116
        // insert item in the db
117
        $item = $this->crud->create(\Request::except(['redirect_after_save', 'password']));
118
119
        //encrypt password
120
        if ($request->input('password')) {
121
            $item->password = bcrypt($request->input('password'));
122
            $item->save();
123
        }
124
125
        // show a success message
126
        \Alert::success(trans('backpack::crud.insert_success'))->flash();
127
128
        // redirect the user where he chose to be redirected
129
        switch (\Request::input('redirect_after_save')) {
130
            case 'current_item_edit':
131
                return \Redirect::to($this->crud->route.'/'.$item->id.'/edit');
132
133
            default:
134
                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...
135
        }
136
    }
137
138
    public function update(UpdateRequest $request)
139
    {
140
        //encrypt password and set it to request
141
        $this->crud->hasAccessOrFail('update');
142
143
        $dataToUpdate = \Request::except(['redirect_after_save', 'password']);
144
145
        //encrypt password
146
        if ($request->input('password')) {
147
            $dataToUpdate['password'] = bcrypt($request->input('password'));
148
        }
149
150
        // update the row in the db
151
        $this->crud->update(\Request::get('id'), $dataToUpdate);
152
153
        // show a success message
154
        \Alert::success(trans('backpack::crud.update_success'))->flash();
155
156
        return \Redirect::to($this->crud->route);
157
    }
158
}
159