Cancelled
Push — test-branch ( 5c534a )
by Yonathan
06:17 queued 06:17
created

UserCrudController::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 63
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 63
ccs 0
cts 26
cp 0
rs 9.1563
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2

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 App\Http\Controllers\Admin;
4
5
use Backpack\CRUD\app\Http\Controllers\CrudController;
6
use App\Models\UserRole;
7
8
class UserCrudController extends CrudController
9
{
10
    /**
11
     * Prepare the admin interface by setting the associated
12
     * model, setting the route, and adding custom columns/fields.
13
     *
14
     * @return void
15
     */
16
    public function setup() : void
17
    {
18
        $this->crud->setModel("App\Models\User");
19
        $this->crud->setRoute("admin/user");
20
        $this->crud->setEntityNameStrings('user', 'users');
21
22
        $this->crud->denyAccess('create');
23
        $this->crud->denyAccess('delete');
24
25
        // Add the custom blade button found in resources/views/vendor/backpack/crud/buttons/profile_edit.blade.php
26
        $this->crud->addButtonFromView('line', 'profile_edit', 'profile_edit', 'end');
27
28
        $this->crud->addColumn([
29
            'name' => 'name',
30
            'type' => 'text',
31
            'label' => 'Name'
32
        ]);
33
        $this->crud->addColumn([
34
            'name' => 'email',
35
            'type' => 'text',
36
            'label' => 'Email'
37
        ]);
38
        $this->crud->addColumn([
39
            'name' => 'user_role.name',
40
            'type' => 'text',
41
            'label' => 'Role'
42
        ]);
43
        $this->crud->addColumn([
44
            'name' => 'is_priority',
45
            'type' => 'check',
46
            'label' => 'Priority'
47
        ]);
48
49
        $this->crud->addFilter([
50
            'name' => 'user_role',
51
            'type' => 'select2',
52
            'label' => 'Role'
53
        ], function () {
54
            return UserRole::all()->keyBy('id')->pluck('name', 'id')->toArray();
55
        }, function ($value) : void {
56
            $this->crud->addClause('where', 'user_role_id', $value);
57
        });
58
59
        $this->crud->addField([
60
            'name' => 'name',
61
            'label' => "Name",
62
            'type' => 'text',
63
            'attributes' => [
64
                'readonly'=>'readonly'
65
            ]
66
        ]);
67
        $this->crud->addField([
68
            'label' => "Role",
69
            'type' => 'select',
70
            'name' => 'user_role_id', // the db column for the foreign key
71
            'entity' => 'user_role', // the method that defines the relationship in your Model
72
            'attribute' => 'name', // foreign key attribute that is shown to user
73
            'model' => "App\Models\UserRole" // foreign key model
74
        ]);
75
        $this->crud->addField([
76
            'name' => 'is_priority',
77
            'type' => 'checkbox',
78
            'label' => 'Priority'
79
        ]);
80
    }
81
82
    /**
83
     * Action for updating an existing User in the database.
84
     *
85
     * @param \Illuminate\Http\Request $request Incoming form request.
86
     *
87
     * @return \Illuminate\Http\RedirectResponse
88
     */
89
    public function update($request) // phpcs:ignore
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

89
    public function update(/** @scrutinizer ignore-unused */ $request) // phpcs:ignore

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        $response = parent::updateCrud();
92
        return $response;
93
    }
94
}
95