Passed
Push — master ( 6ceee8...d4902a )
by Tristan
20:51 queued 08:57
created

UserCrudController::setupListOperation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1.0001

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 51
ccs 17
cts 18
cp 0.9444
rs 9.264
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1.0001

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
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Http\C...perations\ListOperation requires some properties which are not provided by App\Http\Controllers\Admin\UserCrudController: $model, $query, $entity_name_plural
Loading history...
11
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
0 ignored issues
show
introduced by
The trait Backpack\CRUD\app\Http\C...rations\UpdateOperation requires some properties which are not provided by App\Http\Controllers\Admin\UserCrudController: $model, $entity_name
Loading history...
12
13
    /**
14
     * Prepare the admin interface by setting the associated
15
     * model, setting the route, and adding custom columns/fields.
16 1
     *
17
     * @return void
18 1
     */
19 1
    public function setup() : void
20 1
    {
21
        $this->crud->setModel('App\Models\User');
22 1
        $this->crud->setRoute('admin/user');
23 1
        $this->crud->setEntityNameStrings('user', 'users');
24
    }
25 1
26 1
    public function setupListOperation()
27
    {
28
        $this->crud->addColumn([
29
            'name' => 'id',
30 1
            'type' => 'number',
31 1
            'label' => 'ID'
32
        ]);
33
        $this->crud->addColumn([
34
            'name' => 'first_name',
35 1
            'type' => 'text',
36 1
            'label' => 'First Name'
37
        ]);
38
        $this->crud->addColumn([
39
            'name' => 'last_name',
40 1
            'type' => 'text',
41 1
            'label' => 'Last Name'
42
        ]);
43
        $this->crud->addColumn([
44
            'name' => 'user_role.name',
45
            'type' => 'text',
46 1
            'key' => 'user_role_name',
47 1
            'label' => 'Role'
48
        ]);
49
        $this->crud->addColumn([
50
            'name' => 'email',
51 1
            'type' => 'text',
52
            'label' => 'Email'
53
        ]);
54 1
        $this->crud->addColumn([
55
            'name' => 'gov_email',
56 1
            'type' => 'text',
57 1
            'label' => 'Government Email'
58
        ]);
59
        $this->crud->addColumn([
60
            'name' => 'not_in_gov',
61
            'type' => 'check',
62
            'label' => 'In Government'
63
        ]);
64 1
        $this->crud->addColumn([
65 1
            'name' => 'is_priority',
66
            'type' => 'check',
67
            'label' => 'Priority'
68
        ]);
69
        $this->crud->addFilter([
70
            'name' => 'user_role',
71
            'type' => 'select2',
72 1
            'label' => 'Role'
73 1
            ], function () {
74
                return UserRole::all()->keyBy('id')->pluck('name', 'id')->toArray();
75
            }, function ($value) : void {
76
                $this->crud->addClause('where', 'user_role_id', $value);
77 1
            });
78
    }
79
80
    public function setupUpdateOperation()
81
    {
82
        $this->crud->addField([
83
            'name' => 'name',
84
            'label' => 'Name',
85
            'type' => 'text',
86
            'attributes' => [
87
                'readonly'=>'readonly'
88
            ]
89
        ]);
90
        $this->crud->addField([
91
            'label' => 'Role',
92
            'type' => 'select',
93
            'name' => 'user_role_id', // The db column for the foreign key.
94
            'entity' => 'user_role', // The method that defines the relationship in your Model.
95
            'attribute' => 'name', // Foreign key attribute that is shown to user.
96
            'model' => 'App\Models\UserRole' // Foreign key model.
97
        ]);
98
        $this->crud->addField([
99
            'name' => 'is_priority',
100
            'type' => 'checkbox',
101
            'label' => 'Priority'
102
        ]);
103
    }
104
}
105