Passed
Push — feature/azure-webapp-pipeline-... ( f7c88d...cc7783 )
by Grant
05:46 queued 13s
created

JobPosterCrudController::update()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 51
rs 8.5066
cc 7
nc 8
nop 0

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 Jenssegers\Date\Date;
6
use App\Models\Lookup\Department;
7
use App\Models\Lookup\JobPosterStatus;
8
use App\Services\JobStatusTransitionManager;
9
use Backpack\CRUD\app\Http\Controllers\CrudController;
10
use Illuminate\Support\Facades\App;
11
12
class JobPosterCrudController extends CrudController
13
{
14
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
15
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation {
16
        update as traitUpdate;
17
    }
18
19
    /**
20
     * Prepare the admin interface by setting the associated
21
     * model, setting the route, and adding custom columns/fields.
22
     *
23
     * @return void
24
     */
25
    public function setup(): void
26
    {
27
        $this->crud->setModel('App\Models\JobPoster');
28
        $this->crud->setRoute('admin/job-poster');
29
        $this->crud->setEntityNameStrings('Job Poster', 'Job Posters');
30
31
        if (!$this->request->has('order')) {
32
            $this->crud->orderBy('close_date_time', 'desc');
33
        }
34
    }
35
36
    public function setupListOperation()
37
    {
38
        // Required for order logic.
39
        $locale = 'en';
40
        if (null !== $this->request->input('locale')) {
41
            $locale = $this->request->input('locale');
42
        }
43
        App::setLocale($locale);
44
45
        // Add the custom blade buttons found in resources/views/vendor/backpack/crud/buttons/.
46
        $this->crud->addButtonFromView('line', 'job_admin_edit', 'job_admin_edit', 'end');
47
        $this->crud->addButtonFromView('line', 'spb_link', 'spb_link', 'end');
48
        $this->crud->addButtonFromView('line', 'jpb_link', 'jpb_link', 'end');
49
        $this->crud->addButtonFromView('line', 'job_poster_link', 'job_poster_link', 'end');
50
        $this->crud->addButtonFromView('line', 'applicants_download', 'applicants_download', 'end');
51
52
        $this->crud->addColumn([
53
            'name' => 'id',
54
            'type' => 'number',
55
            'label' => 'ID'
56
        ]);
57
        $this->crud->addColumn([
58
            'name' => 'title',
59
            'type' => 'text',
60
            'label' => 'Title',
61
            'searchLogic' => function ($query, $column, $searchTerm) use ($locale): void {
62
                $query->orWhere('title->' . $locale, 'ilike', "%$searchTerm%");
63
            },
64
            'orderLogic' => function ($query, $column, $columnDirection) use ($locale) {
65
                return $query->orderBy('title->' . $locale, $columnDirection)->select('*');
66
            }
67
        ]);
68
        $this->crud->addColumn([
69
            'name' => 'job_poster_status.key',
70
            'label' => 'Status',
71
            'type' => 'text',
72
            'orderable' => true,
73
            'orderLogic' => function ($query, $column, $columnDirection) {
74
                return $query->leftJoin('job_poster_status', 'job_poster_status.id', '=', 'job_posters.job_poster_status_id')
75
                    ->orderBy('job_poster_status.key', $columnDirection)->select('job_posters.*');
76
            }
77
        ]);
78
        $this->crud->addColumn([
79
            'name' => 'isOpen',
80
            'label' => 'Open',
81
            'type' => 'closure',
82
            'orderable' => false,
83
            'function' => function ($entry) {
84
                return $entry->isOpen() ?
85
                    '<span><i class="fa fa-check-circle"></i></span>' :
86
                    '<span><i class="fa fa-circle"></i></span>';
87
            }
88
        ]);
89
        $this->crud->addColumn([
90
            'name' => 'isClosed',
91
            'label' => 'Closed',
92
            'type' => 'closure',
93
            'orderable' => false,
94
            'function' => function ($entry) {
95
                return $entry->isClosed() ?
96
                    '<span><i class="fa fa-check-circle"></i></span>' :
97
                    '<span><i class="fa fa-circle"></i></span>';
98
            }
99
        ]);
100
101
        $this->crud->addColumn([
102
            'name' => 'internal_only',
103
            'label' => 'Internal Only',
104
            'type' => 'check',
105
        ]);
106
107
        $this->crud->addColumn([
108
            'name' => 'manager_user_name',
109
            'type' => 'closure',
110
            'label' => 'Manager',
111
            'orderable' => false,
112
            'function' => function ($entry) {
113
                return '<a href="' . route('manager.profile.edit', $entry->manager->id) . '" target="_blank">' . $entry->manager->user->full_name . '</a>';
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

113
                return '<a href="' . /** @scrutinizer ignore-call */ route('manager.profile.edit', $entry->manager->id) . '" target="_blank">' . $entry->manager->user->full_name . '</a>';
Loading history...
114
            }
115
        ]);
116
        $this->crud->addColumn([
117
            'name' => 'department.name',
118
            'label' => 'Department',
119
            'type' => 'text'
120
        ]);
121
        $this->crud->addColumn([
122
            'name' => 'submitted_applications_count',
123
            'label' => 'Applications',
124
            'type' => 'closure',
125
            'function' =>
126
            function ($entry) {
127
                return $entry->submitted_applications_count() > 0 ?
128
                    '<a target="_blank" href="' . route('manager.jobs.applications', $entry->id) . '">' . $entry->submitted_applications_count() . ' (View <i class="fa fa-external-link"></i>)</a>' :
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

128
                    '<a target="_blank" href="' . /** @scrutinizer ignore-call */ route('manager.jobs.applications', $entry->id) . '">' . $entry->submitted_applications_count() . ' (View <i class="fa fa-external-link"></i>)</a>' :
Loading history...
129
                    $entry->submitted_applications_count();
130
            }
131
        ]);
132
133
        // Filters.
134
        $this->crud->addFilter([
135
            'name' => 'departments',
136
            'type' => 'select2_multiple',
137
            'label' => 'Filter by department'
138
        ], function () {
139
            return Department::all()->pluck('name', 'id')->toArray();
140
        }, function ($values) {
141
            $this->crud->addClause('WhereHas', 'department', function ($query) use ($values) {
142
                foreach (json_decode($values) as $key => $value) {
143
                    if ($key === 0) {
144
                        $query->where('id', $value);
145
                    } else {
146
                        $query->orWhere('id', $value);
147
                    }
148
                }
149
            });
150
        });
151
152
        $this->crud->addFilter([
153
            'name' => 'statuses',
154
            'type' => 'select2_multiple',
155
            'label' => 'Filter by status'
156
        ], function () {
157
            // Using key because some of the job status names are the same.
158
            return JobPosterStatus::all()->pluck('key', 'id')->toArray();
159
        }, function ($values) {
160
            $this->crud->addClause('WhereHas', 'job_poster_status', function ($query) use ($values) {
161
                foreach (json_decode($values) as $key => $value) {
162
                    if ($key === 0) {
163
                        $query->where('id', $value);
164
                    } else {
165
                        $query->orWhere('id', $value);
166
                    }
167
                }
168
            });
169
        });
170
    }
171
172
    public function setupUpdateOperation()
173
    {
174
        $this->crud->addField([
175
            'name' => 'title',
176
            'label' => 'Title',
177
            'type' => 'text',
178
            'attributes' => [
179
                'readonly' => 'readonly'
180
            ]
181
        ]);
182
        $this->crud->addField([
183
            'name' => 'salary_min',
184
            'type' => 'number',
185
            'label' => 'Minimum Salary',
186
        ]);
187
        $this->crud->addField([
188
            'name' => 'salary_max',
189
            'type' => 'number',
190
            'label' => 'Maximum Salary',
191
        ]);
192
        $this->crud->addField([
193
            'name' => 'noc',
194
            'type' => 'number',
195
            'label' => 'NOC Code',
196
        ]);
197
        $this->crud->addField([
198
            'name' => 'open_date_time',
199
            'label' => 'Open Date',
200
            'type' => 'date_picker',
201
            'date_picker_options' => [
202
                'todayBtn' => 'linked',
203
                'format' => 'yyyy-mm-dd',
204
            ],
205
        ]);
206
        $this->crud->addField([
207
            'name' => 'close_date_time',
208
            'label' => 'Close Date',
209
            'type' => 'date_picker',
210
            'date_picker_options' => [
211
                'todayBtn' => 'linked',
212
                'format' => 'yyyy-mm-dd',
213
            ],
214
        ]);
215
        $this->crud->addField([
216
            'name' => 'start_date_time',
217
            'label' => 'Start Date',
218
            'type' => 'date_picker',
219
            'date_picker_options' => [
220
                'todayBtn' => 'linked',
221
                'format' => 'yyyy-mm-dd',
222
            ],
223
        ]);
224
        $this->crud->addField([
225
            'name' => 'process_number',
226
            'type' => 'text',
227
            'label' => 'Process #',
228
        ]);
229
        $this->crud->addField([
230
            'name' => 'priority_clearance_number',
231
            'type' => 'number',
232
            'label' => 'Priority Clearance #',
233
        ]);
234
        $this->crud->addField([
235
            'name' => 'loo_issuance_date',
236
            'type' => 'date_picker',
237
            'label' => 'Letter of Offer Issuance Date',
238
            'date_picker_options' => [
239
                'todayBtn' => 'linked',
240
                'format' => 'yyyy-mm-dd',
241
            ],
242
        ]);
243
244
        $this->crud->addField([
245
            'name' => 'internal_only',
246
            'type' => 'checkbox',
247
            'label' => 'Internal Only (Do not list this poster on the Browse Jobs page. You must access it with the direct URL.)',
248
        ]);
249
250
        $transitionManager = new JobStatusTransitionManager();
251
        $job = $this->crud->getCurrentEntry();
252
        $legalDestinations = $transitionManager->legalDestinations($job->job_poster_status->key);
253
        $validStatuses = JobPosterStatus::all()->filter(function ($status) use ($job, $legalDestinations) {
254
            return in_array($status->key, $legalDestinations) || $status->id === $job->job_poster_status_id;
255
        });
256
        $statusOptions = $validStatuses->mapWithKeys(function ($status) {
257
            return [$status->id => $status->key];
258
        });
259
        $this->crud->addField([
260
            'name' => 'job_poster_status_id',
261
            'label' => 'Status',
262
            'type' => 'select_from_array',
263
            'options' => $statusOptions,
264
            'allows_null' => false,
265
            'default' => $job->job_poster_status_id,
266
        ]);
267
268
        // Strategic Talent Response fields
269
        $this->crud->addField([
270
            'label' => 'Talent Stream',
271
            'type' => 'select',
272
            'name' => 'talent_stream_id', // the db column for the foreign key
273
            'entity' => 'talent_stream', // the method that defines the relationship in your Model
274
            'attribute' => 'name', // foreign key attribute that is shown to user
275
        ]);
276
        $this->crud->addField([
277
            'label' => 'Talent Stream Subcategory',
278
            'type' => 'select',
279
            'name' => 'talent_stream_category_id', // the db column for the foreign key
280
            'entity' => 'talent_stream_category', // the method that defines the relationship in your Model
281
            'attribute' => 'name', // foreign key attribute that is shown to user
282
        ]);
283
        $this->crud->addField([
284
            'label' => 'Job Skill Level',
285
            'type' => 'select',
286
            'name' => 'job_skill_level_id', // the db column for the foreign key
287
            'entity' => 'job_skill_level', // the method that defines the relationship in your Model
288
            'attribute' => 'name', // foreign key attribute that is shown to user
289
        ]);
290
    }
291
292
    public function update()
293
    {
294
        $open_date_time = $this->crud->request->request->get('open_date_time');
295
        $close_date_time = $this->crud->request->request->get('close_date_time');
296
        $start_date_time = $this->crud->request->request->get('start_date_time');
297
298
        $job = $this->crud->getCurrentEntry();
299
300
        $open_date_current = new Date("$job->open_date_time");
301
        $close_date_current = new Date("$job->close_date_time");
302
        $start_date_current = new Date("$job->start_date_time");
303
304
        if ($open_date_current->format('Y-m-d') !== $open_date_time) {
305
            $this->crud->request->request->remove('open_date_time');
306
            // Manipulates the input field to save the "start of day" timestamp.
307
            $this->crud->request->request->add([
308
                'open_date_time' => $open_date_time !== null ? ptDayStartToUtcTime($open_date_time) : null
309
            ]);
310
        } else {
311
            $this->crud->request->request->add([
312
                'open_date_time' => $open_date_current
313
            ]);
314
        }
315
316
        if ($close_date_current->format('Y-m-d') !== $close_date_time) {
317
            $this->crud->request->request->remove('close_date_time');
318
            // Manipulates the input field to save the "end of day" timestamp.
319
            $this->crud->request->request->add([
320
                'close_date_time' => $close_date_time !== null ? ptDayEndToUtcTime($close_date_time) : null
321
            ]);
322
        } else {
323
            $this->crud->request->request->add([
324
                'close_date_time' => $close_date_current
325
            ]);
326
        }
327
328
        if ($start_date_current->format('Y-m-d') !== $start_date_time) {
329
            $this->crud->request->request->remove('start_date_time');
330
            // Manipulates the input field to save the "start of day" timestamp.
331
            $this->crud->request->request->add([
332
                'start_date_time' => $start_date_time !== null ? ptDayStartToUtcTime($start_date_time) : null
333
            ]);
334
        } else {
335
            $this->crud->request->request->add([
336
                'start_date_time' => $start_date_current
337
            ]);
338
        }
339
340
        $response = $this->traitUpdate();
341
342
        return $response;
343
    }
344
}
345