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

SkillCrudController::setupListOperation()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 105
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3.054

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 68
c 4
b 0
f 0
dl 0
loc 105
ccs 18
cts 22
cp 0.8182
rs 8.6981
cc 3
nc 2
nop 0
crap 3.054

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
// Validation.
6
use App\Http\Requests\SkillCrudRequest as StoreRequest;
7
use App\Http\Requests\SkillCrudRequest as UpdateRequest;
8
use App\Models\Classification;
9
use App\Models\Lookup\SkillType;
10
use Backpack\CRUD\app\Http\Controllers\CrudController;
11
use Illuminate\Support\Facades\App;
12
13
class SkillCrudController extends CrudController
14
{
15
    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\SkillCrudController: $model, $entity_name_plural
Loading history...
16
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
0 ignored issues
show
Bug introduced by
The trait Backpack\CRUD\app\Http\C...rations\CreateOperation requires the property $entity_name which is not provided by App\Http\Controllers\Admin\SkillCrudController.
Loading history...
17
    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\SkillCrudController: $model, $entity_name
Loading history...
18
19
    /**
20 1
     * Prepare the admin interface by setting the associated
21
     * model, setting the route, and adding custom columns/fields.
22
     *
23
     * @return void
24 1
     */
25 1
    public function setup() : void
26
    {
27
        // Eloquent model to associate with this collection
28 1
        // of views and controller actions.
29
        $this->crud->setModel('App\Models\Skill');
30
        // Custom backpack route.
31
        $this->crud->setRoute('admin/skill');
32 1
        // Custom strings to display within the backpack UI,
33
        // things like Create Skill, Delete Skills, etc.
34 1
        $this->crud->setEntityNameStrings('skill', 'skills');
35
36
        $this->crud->operation(['create', 'update'], function () {
37 1
            // Add custom fields to the create/update views.
38
            $this->crud->addField([
39
                'name' => 'name',
40 1
                'type' => 'text',
41 1
                'label' => 'Name',
42 1
            ]);
43 1
44
            $this->crud->addField([
45
                'name' => 'description',
46 1
                'type' => 'textarea',
47
                'label' => 'Description',
48
                'limit' => 70,
49 1
            ]);
50
51
            $this->crud->addField([
52 1
                'name' => 'skill_type_id',
53 1
                'label' => 'Type',
54 1
                'type' => 'select_from_array',
55 1
                'options' => SkillType::all()->pluck('name', 'id')->toArray(),
56
                'allow_null' => false,
57
            ]);
58 1
59
            $this->crud->addField([
60
                'name' => 'classifications',
61
                'type' => 'select2_multiple',
62 1
                'label' => 'Classifications (select all that apply)',
63 1
                'entity' => 'skills',
64 1
                'attribute' => 'key',
65 1
                'model' => 'App\Models\Classification',
66
                'pivot' => true,
67
            ]);
68
69 1
            $this->crud->addField([
70
                'name' => 'is_culture_skill',
71
                'label' => 'This is a culture skill',
72 1
                'type' => 'checkbox'
73 1
            ]);
74
75
            $this->crud->addField([
76
                'name' => 'is_future_skill',
77
                'label' => 'This is a future skill',
78
                'type' => 'checkbox'
79
            ]);
80
        });
81 1
    }
82 1
83
    public function setupListOperation()
84
    {
85
        // Workaround for how the unique_translation validation
86
        // works in App\Http\Requests\SkillCrudRequest.
87
        $locale = 'en';
88 1
        if (null !== $this->request->input('locale')) {
89 1
            $locale = $this->request->input('locale');
90
        }
91
        App::setLocale($locale);
92
93
        // Remove delete button.
94
        $this->crud->removeButton('delete');
95
96 1
        // Add custom columns to the Skill index view.
97 1
        $this->crud->addColumn([
98
            'name' => 'id',
99
            'type' => 'text',
100
            'label' => 'ID',
101
            'orderable' => true
102 1
        ]);
103 1
104
        $this->crud->addColumn([
105
            'name' => 'name',
106
            'type' => 'text',
107
            'label' => 'Name',
108 1
            'searchLogic' => function ($query, $column, $searchTerm) use ($locale) : void {
109 1
                $query->orWhere('name->' . $locale, 'like', "%$searchTerm%");
110 1
            },
111 1
            'orderLogic' => function ($query, $column, $columnDirection) use ($locale) {
112 1
                return $query->orderBy('name->' . $locale, $columnDirection)->select('*');
113
            }
114
        ]);
115
116 1
        $this->crud->addColumn([
117 1
            'name' => 'description',
118
            'type' => 'text',
119
            'label' => 'Description',
120
            'searchLogic' => function ($query, $column, $searchTerm) use ($locale) : void {
121
                $query->orWhere('description->' . $locale, 'like', "%$searchTerm%");
122
            },
123
            'orderable' => false,
124
        ]);
125
126 1
        $this->crud->addColumn([
127 1
            'name' => 'skill_type.name',
128
            'key' => 'skill_type_name',
129
            'type' => 'text',
130
            'label' => 'Type',
131
            'orderable' => true,
132 1
            'orderLogic' => function ($query, $column, $columnDirection) use ($locale) {
0 ignored issues
show
Unused Code introduced by
The import $locale is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
133 1
                return $query->orderBy('skill_type_id', $columnDirection)->select('*');
134
            }
135
        ]);
136
137 1
        $this->crud->addColumn([
138
            'label' => 'Classifications',
139
            'type' => 'select_multiple',
140
            'name' => 'classifications',
141
            'entity' => 'classifications',
142
            'attribute' => 'key',
143
            'model' => 'App\Models\Skill',
144
        ]);
145
146
        $this->crud->addColumn([
147
            'name' => 'is_culture_skill',
148
            'label' => 'Culture',
149
            'type' => 'boolean',
150
            'orderable' => true,
151
        ]);
152
153
        $this->crud->addColumn([
154
            'name' => 'is_future_skill',
155
            'label' => 'Future',
156
            'type' => 'boolean',
157
            'orderable' => true,
158
        ]);
159
160
        // Add select2_multiple filter for classifications.
161
        $this->crud->addFilter([
162
            'name' => 'classifications',
163
            'key' => 'classifications_filter',
164
            'type' => 'select2_multiple',
165
            'label' => 'Filter by classification'
166
        ], function () {
167
            // The options that show up in the select2.
168
            return Classification::all()->pluck('key', 'id')->toArray();
169
        }, function ($values) {
170
            // If the filter is active.
171
            foreach (json_decode($values) as $key => $value) {
172
                $this->crud->query = $this->crud->query->whereHas('classifications', function ($query) use ($value) {
173
                    $query->where('id', $value);
174
                });
175
            }
176
        });
177
178
        // Add filter for skills without classifications.
179
        $this->crud->addFilter(
180
            [
181
                'type' => 'simple',
182
                'name' => 'noClassification',
183
                'label'=> 'No classification'
184
            ],
185
            false,
186
            function () {
187
                $this->crud->query = $this->crud->query->doesntHave('classifications');
188
            }
189
        );
190
    }
191
192
    public function setupCreateOperation()
193
    {
194
        $this->crud->setValidation(StoreRequest::class);
195
    }
196
197
    public function setupUpdateOperation()
198
    {
199
        $this->crud->setValidation(UpdateRequest::class);
200
    }
201
}
202