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

JobPosterCrudController::setupUpdateOperation()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 78
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 78
ccs 0
cts 2
cp 0
rs 8.8727
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 12

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 App\Models\Lookup\Department;
6
use Backpack\CRUD\app\Http\Controllers\CrudController;
7
8
class JobPosterCrudController 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\JobPosterCrudController: $model, $query, $entity_name_plural
Loading history...
11
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { update as traitUpdate;
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\JobPosterCrudController: $model, $entity_name
Loading history...
12
    }
13
14
    /**
15 1
     * Prepare the admin interface by setting the associated
16
     * model, setting the route, and adding custom columns/fields.
17 1
     *
18 1
     * @return void
19 1
     */
20
    public function setup() : void
21 1
    {
22 1
        $this->crud->setModel('App\Models\JobPoster');
23
        $this->crud->setRoute('admin/job-poster');
24 1
        $this->crud->setEntityNameStrings('Job Poster', 'Job Posters');
25 1
26
        if (!$this->request->has('order')) {
27
            $this->crud->orderBy('close_date_time', 'desc');
28
        }
29 1
    }
30
31 1
    public function setupListOperation()
32 1
    {
33
        // Add the custom blade buttons found in resources/views/vendor/backpack/crud/buttons/.
34
        $this->crud->addButtonFromView('line', 'job_admin_edit', 'job_admin_edit', 'end');
35
        $this->crud->addButtonFromView('line', 'spb_link', 'spb_link', 'end');
36 1
        $this->crud->addButtonFromView('line', 'jpb_link', 'jpb_link', 'end');
37 1
        $this->crud->addButtonFromView('line', 'job_poster_link', 'job_poster_link', 'end');
38
39
40
        $this->crud->addColumn([
41 1
            'name' => 'id',
42 1
            'type' => 'number',
43
            'label' => 'ID'
44
        ]);
45
        $this->crud->addColumn([
46 1
            'name' => 'title',
47 1
            'type' => 'text',
48
            'label' => 'Title'
49
        ]);
50
        $this->crud->addColumn([
51
            'name' => 'status',
52 1
            'label' => 'Status',
53 1
            'type' => 'model_function',
54
            'function_name' => 'status'
55
        ]);
56
        $this->crud->addColumn([
57 1
            'name' => 'published',
58 1
            'label' => 'Published',
59
            'type' => 'check',
60
        ]);
61
        $this->crud->addColumn([
62 1
            'name' => 'manager_user_name',
63 1
            'type' => 'closure',
64 1
            'label' => 'Manager',
65 1
            'orderable' => false,
66
            'function' => function ($entry) {
67
                return '<a href="' . route('manager.profile.edit', $entry->manager->user->id) . '" target="_blank">' . $entry->manager->user->full_name . '</a>';
68
            }
69
        ]);
70
        $this->crud->addColumn([
71 1
            'name' => 'department.name',
72
            'label' => 'Department',
73
            'type' => 'text'
74 1
        ]);
75 1
        $this->crud->addColumn([
76
            'name' => 'submitted_applications_count',
77
            'label' => 'Total Applications',
78
            'type' => 'model_function',
79
            'function_name' => 'submitted_applications_count'
80
        ]);
81
82 1
        // Filters.
83 1
        $this->crud->addFilter([
84
            'name' => 'departments',
85
            'type' => 'select2_multiple',
86
            'label' => 'Departments'
87
        ], function () {
88
            return Department::all()->pluck('name', 'id')->toArray();
89
        }, function ($values) {
90 1
            $this->crud->addClause('WhereHas', 'department', function ($query) use ($values) {
91 1
                foreach (json_decode($values) as $key => $value) {
92 1
                    if ($key === 0) {
93
                        $query->where('id', $value);
94
                    } else {
95
                        $query->orWhere('id', $value);
96
                    }
97 1
                }
98
            });
99
        });
100
    }
101
102
    public function setupUpdateOperation()
103
    {
104
        $this->crud->addField([
105
            'name' => 'title',
106
            'label' => 'Title',
107
            'type' => 'text',
108
            'attributes' => [
109
                'readonly' => 'readonly'
110
            ]
111
        ]);
112
        $this->crud->addField([
113
            'name' => 'salary_min',
114
            'type' => 'number',
115
            'label' => 'Minimum Salary',
116
        ]);
117
        $this->crud->addField([
118
            'name' => 'salary_max',
119
            'type' => 'number',
120
            'label' => 'Maximum Salary',
121
        ]);
122
        $this->crud->addField([
123
            'name' => 'noc',
124
            'type' => 'number',
125
            'label' => 'NOC Code',
126
        ]);
127
        $this->crud->addField([
128
            'name' => 'open_date_time',
129
            'label' => 'Open Date',
130
            'type' => 'date_picker',
131
            'date_picker_options' => [
132
                'todayBtn' => 'linked',
133
                'format' => 'yyyy-mm-dd',
134
            ],
135
        ]);
136
        $this->crud->addField([
137
            'name' => 'close_date_time',
138
            'label' => 'Close Date',
139
            'type' => 'date_picker',
140
            'date_picker_options' => [
141
                'todayBtn' => 'linked',
142
                'format' => 'yyyy-mm-dd',
143
            ],
144
        ]);
145
        $this->crud->addField([
146
            'name' => 'start_date_time',
147
            'label' => 'Start Date',
148
            'type' => 'date_picker',
149
            'date_picker_options' => [
150
                'todayBtn' => 'linked',
151
                'format' => 'yyyy-mm-dd',
152
            ],
153
        ]);
154
        $this->crud->addField([
155
            'name' => 'process_number',
156
            'type' => 'number',
157
            'label' => 'Process #',
158
        ]);
159
        $this->crud->addField([
160
            'name' => 'priority_clearance_number',
161
            'type' => 'number',
162
            'label' => 'Priority Clearance #',
163
        ]);
164
        $this->crud->addField([
165
            'name' => 'loo_issuance_date',
166
            'type' => 'date_picker',
167
            'label' => 'Letter of Offer Issuance Date',
168
            'date_picker_options' => [
169
                'todayBtn' => 'linked',
170
                'format' => 'yyyy-mm-dd',
171
            ],
172
        ]);
173
        if ($this->crud->getCurrentEntry() &&
174
            !$this->crud->getCurrentEntry()->published
175
        ) {
176
            $this->crud->addField([
177
                'name' => 'published',
178
                'label' => 'Publish',
179
                'type' => 'checkbox'
180
            ]);
181
        }
182
    }
183
184
    public function update()
185
    {
186
        $open_date = $this->crud->request->request->get('open_date_time');
187
        $close_date = $this->crud->request->request->get('close_date_time');
188
        $start_date = $this->crud->request->request->get('start_date_time');
189
        $this->crud->request->request->remove('open_date_time');
190
        $this->crud->request->request->remove('close_date_time');
191
        $this->crud->request->request->remove('start_date_time');
192
        // Manipulates the input fields to save the "end of day" timestamp for
193
        // open/close/start dates.
194
        $this->crud->request->request->add([
195
            'open_date_time' => ptDayStartToUtcTime($open_date),
196
            'close_date_time' => ptDayEndToUtcTime($close_date),
197
            'start_date_time' => ptDayStartToUtcTime($start_date),
198
        ]);
199
        $response = $this->traitUpdate();
200
201
        return $response;
202
    }
203
}
204