Passed
Push — master ( 795d23...149f73 )
by Grant
06:52 queued 12s
created

JobController::downloadApplicants()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 51
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 5.0067

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 51
ccs 29
cts 31
cp 0.9355
rs 9.0968
cc 5
nc 8
nop 1
crap 5.0067

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;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\JobApplication;
7
use App\Models\JobPoster;
8
use App\Models\JobPosterQuestion;
9
use App\Models\Lookup\ApplicationStatus;
10
use App\Models\Lookup\CitizenshipDeclaration;
11
use App\Models\Lookup\JobPosterStatus;
12
use App\Models\Lookup\VeteranStatus;
13
use App\Models\Manager;
14
use App\Services\JobPosterDefaultQuestions;
15
use App\Services\Validation\JobPosterValidator;
16
use Carbon\Carbon;
17
use Facades\App\Services\WhichPortal;
18
use Illuminate\Http\Request;
19
use Illuminate\Support\Facades\Auth;
20
use Illuminate\Support\Facades\Lang;
21
use Illuminate\Support\Facades\Response;
22
use Illuminate\Support\Facades\Validator;
23
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
24
25
class JobController extends Controller
26
{
27
    /**
28
     * Display a listing of JobPosters.
29
     *
30
     * @return \Illuminate\Http\Response
31
     */
32
    public function index()
33
    {
34
        // If true, show the Paused due to COVID-19 message.
35
        $emergency_response = config('seasonal.is_covid_emergency');
0 ignored issues
show
Bug introduced by
The function config 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

35
        $emergency_response = /** @scrutinizer ignore-call */ config('seasonal.is_covid_emergency');
Loading history...
36
37
        // Find published jobs that are currently open for applications.
38
        // Eager load required relationships: Department, Province, JobTerm.
39
        // Eager load the count of submitted applications, to prevent the relationship
40
        // from being actually loaded and firing off events.
41
        $jobs = JobPoster::where('internal_only', false)
42
            ->where('department_id', '!=', config('app.strategic_response_department_id'))
43
            ->where('job_poster_status_id', JobPosterStatus::where('key', 'live')->first()->id)
44
            ->with([
45
                'department',
46
                'province',
47
                'job_term',
48
            ])
49
            ->withCount([
50
                'submitted_applications',
51
            ])
52
            ->get();
53
54
        $null_alert = $emergency_response
55
            ? Lang::get('applicant/job_index.index.covid_null_alert')
56
            : Lang::get('applicant/job_index.index.null_alert');
57
        return view('applicant/job_index', [
0 ignored issues
show
Bug introduced by
The function view 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

57
        return /** @scrutinizer ignore-call */ view('applicant/job_index', [
Loading history...
58
            'job_index' => Lang::get('applicant/job_index'),
59
            'null_alert' => $null_alert,
60
            'jobs' => $jobs
61
        ]);
62
    }
63
64
    /**
65
     * Display a listing of a manager's JobPosters.
66
     *
67
     * @return \Illuminate\Http\Response
68
     */
69
    public function managerIndex()
70
    {
71
        $manager = Auth::user()->manager;
72
73 1
        $jobs = JobPoster::where('manager_id', $manager->id)
74
            ->with('classification')
75 1
            ->withCount('submitted_applications')
76 1
            ->get();
77 1
78 1
        foreach ($jobs as &$job) {
79
            // If the chosen language is null then set to english.
80 1
            $chosen_lang = $job->chosen_lang ?: 'en';
81
82 1
            // Show chosen lang title if current title is empty.
83
            if (empty($job->title)) {
84 1
                $job->title = $job->getTranslation('title', $chosen_lang);
85
                $job->trans_required = true;
86
            }
87
        }
88
89
        return view('manager/job_index', [
0 ignored issues
show
Bug introduced by
The function view 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

89
        return /** @scrutinizer ignore-call */ view('manager/job_index', [
Loading history...
90
            // Localization Strings.
91
            'jobs_l10n' => Lang::get('manager/job_index'),
92
            // Data.
93
            'jobs' => $jobs,
94
        ]);
95 1
    }
96
97
    /**
98 1
     * Display a listing of a hr advisor's JobPosters.
99 1
     *
100
     * @return \Illuminate\Http\Response
101
     */
102 1
    public function hrIndex(Request $request)
103
    {
104
        $hrAdvisor = $request->user()->hr_advisor;
105 1
        return view('hr_advisor/job_index', [
0 ignored issues
show
Bug introduced by
The function view 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

105
        return /** @scrutinizer ignore-call */ view('hr_advisor/job_index', [
Loading history...
106 1
            'jobs_l10n' => Lang::get('hr_advisor/job_index'),
107 1
            'hr_advisor_id' => $hrAdvisor->id
108
        ]);
109
    }
110
111
112 1
113
114 1
    /**
115 1
     * Delete a draft Job Poster.
116
     *
117
     * @param  \Illuminate\Http\Request $request   Incoming request object.
118
     * @param  \App\Models\JobPoster    $jobPoster Job Poster object.
119
     * @return \Illuminate\Http\Response
120
     */
121
    public function destroy(Request $request, JobPoster $jobPoster)
122
    {
123
        $jobPoster->delete();
124
    }
125
126
    /**
127
     * Display the specified job poster.
128
     *
129
     * @param  \Illuminate\Http\Request $request   Incoming request object.
130
     * @param  \App\Models\JobPoster    $jobPoster Job Poster object.
131
     * @return \Illuminate\Http\Response
132
     */
133
    public function show(Request $request, JobPoster $jobPoster)
134
    {
135
        $jobPoster->load([
136
            'department',
137
            'criteria.skill.skill_type',
138 6
            'manager.team_culture',
139
            'manager.work_environment'
140 6
        ]);
141 6
142
        $user = Auth::user();
143
144
        // TODO: Improve workplace photos, and reference them in template direction from WorkEnvironment model.
145
        $workplacePhotos = [];
146
        foreach ($jobPoster->manager->work_environment->workplace_photo_captions as $photoCaption) {
147 6
            $workplacePhotos[] = [
148
                'description' => $photoCaption->description,
149
                'url' => '/images/user.png'
150 6
            ];
151 6
        }
152
153
        // TODO: replace route('manager.show',manager.id) in templates with link using slug.
154
        $essential = $jobPoster->criteria->filter(
155
            function ($value, $key) {
156
                return $value->criteria_type->name == 'essential';
157
            }
158
        )->sortBy('id');
159
        $asset = $jobPoster->criteria->filter(
160 6
            function ($value, $key) {
161
                return $value->criteria_type->name == 'asset';
162 4
            }
163 6
        )->sortBy('id');
164
        $criteria = [
165 6
            'essential' => $essential,
166
            'asset' => $asset,
167 4
        ];
168 6
169
        $jobLang = Lang::get('applicant/job_post');
170
171
        $applyButton = [];
172 6
        if (WhichPortal::isManagerPortal()) {
173
            $applyButton = [
174 6
                'href' => route('manager.jobs.edit', $jobPoster->id),
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

174
                'href' => /** @scrutinizer ignore-call */ route('manager.jobs.edit', $jobPoster->id),
Loading history...
175 6
                'title' => $jobLang['apply']['edit_link_title'],
176
                'text' => $jobLang['apply']['edit_link_label'],
177 4
            ];
178 4
        } elseif (WhichPortal::isHrPortal()) {
179 4
            if ($jobPoster->hr_advisors->contains('user_id', $user->id)) {
180
                $applyButton = [
181 2
                    'href' => route('hr_advisor.jobs.summary', $jobPoster->id),
182
                    'title' => null,
183 1
                    'text' => Lang::get('hr_advisor/job_summary.summary_title'),
184 1
                ];
185 1
            } else {
186
                $applyButton = [
187 1
                    'href' => route('hr_advisor.jobs.index'),
188
                    'title' => null,
189 1
                    'text' => Lang::get('hr_advisor/job_index.title'),
190 1
                ];
191 1
            }
192
        } elseif (Auth::check() && $jobPoster->isOpen()) {
193
            $application = JobApplication::where('applicant_id', Auth::user()->applicant->id)
194
                ->where('job_poster_id', $jobPoster->id)->first();
195
            // If applicants job application is not draft anymore then link to application preview page.
196
            if ($application != null && $application->application_status->name != 'draft') {
197
                $applyButton = [
198
                    'href' => route('applications.show', $application->id),
199
                    'title' => $jobLang['apply']['view_link_title'],
200
                    'text' => $jobLang['apply']['view_link_label'],
201 6
                ];
202 6
            } else {
203
                $applyButton = [
204 6
                    'href' => route('job.application.edit.1', $jobPoster->id),
205 6
                    'title' => $jobLang['apply']['apply_link_title'],
206 6
                    'text' => $jobLang['apply']['apply_link_label'],
207 6
                ];
208 6
            }
209 6
        } elseif (Auth::guest() && $jobPoster->isOpen()) {
210 6
            $applyButton = [
211 6
                'href' => route('job.application.edit.1', $jobPoster->id),
212 6
                'title' => $jobLang['apply']['login_link_title'],
213 6
                'text' => $jobLang['apply']['login_link_label'],
214
            ];
215
        } else {
216
            $applyButton = [
217
                'href' => null,
218
                'title' => null,
219
                'text' => $jobLang['apply']['job_closed_label'],
220
            ];
221
        }
222
223
        $jpb_release_date = strtotime('2019-08-21 16:18:17');
224 2
        $job_created_at = strtotime($jobPoster->created_at);
225
226 2
        $custom_breadcrumbs = [
227 2
            'home' => route('home'),
228
            'jobs' => route(WhichPortal::prefixRoute('jobs.index')),
229 2
            $jobPoster->title ?: 'job-title-missing' => route(WhichPortal::prefixRoute('jobs.summary'), $jobPoster),
230
            'preview' => '',
231 2
        ];
232 2
233 2
        // If the poster is part of the Strategic Talent Response dept, use the talent stream template.
234
        // Else, If the job poster is created after the release of the JPB.
235
        // Then, render with updated poster template.
236 2
        // Else, render with old poster template.
237
        if ($jobPoster->isInStrategicResponseDepartment()) {
238
            return view(
0 ignored issues
show
Bug introduced by
The function view 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

238
            return /** @scrutinizer ignore-call */ view(
Loading history...
239
                'applicant/strategic_response_job_post',
240
                [
241
                    'job_post' => $jobLang,
242
                    'frequencies' => Lang::get('common/lookup/frequency'),
243
                    'skill_template' => Lang::get('common/skills'),
244
                    'job' => $jobPoster,
245
                    'manager' => $jobPoster->manager,
246
                    'criteria' => $criteria,
247
                    'apply_button' => $applyButton,
248
                    'custom_breadcrumbs' => $custom_breadcrumbs,
249
                ]
250
            );
251
        } elseif ($job_created_at > $jpb_release_date) {
252
            // Updated job poster (JPB).
253
            return view(
254
                'applicant/jpb_job_post',
255
                [
256
                    'job_post' => $jobLang,
257 1
                    'frequencies' => Lang::get('common/lookup/frequency'),
258
                    'skill_template' => Lang::get('common/skills'),
259 1
                    'job' => $jobPoster,
260
                    'manager' => $jobPoster->manager,
261
                    'criteria' => $criteria,
262
                    'apply_button' => $applyButton,
263
                    'custom_breadcrumbs' => $custom_breadcrumbs,
264
                ]
265
            );
266
        } else {
267
            // Old job poster.
268
            return view(
269 1
                'applicant/job_post',
270
                [
271 1
                    'job_post' => $jobLang,
272
                    'frequencies' => Lang::get('common/lookup/frequency'),
273
                    'manager' => $jobPoster->manager,
274 1
                    'manager_profile_photo_url' => '/images/user.png', // TODO get real photo.
275
                    'team_culture' => $jobPoster->manager->team_culture,
276
                    'work_environment' => $jobPoster->manager->work_environment,
277 1
                    'workplace_photos' => $workplacePhotos,
278 1
                    'job' => $jobPoster,
279 1
                    'criteria' => $criteria,
280 1
                    'apply_button' => $applyButton,
281
                    'skill_template' => Lang::get('common/skills'),
282
                    'custom_breadcrumbs' => $custom_breadcrumbs,
283
                ]
284
            );
285
        }
286
    }
287
288
    /**
289
     * Display the form for editing an existing Job Poster
290
     * Only allows editing fields that don't appear on the react-built Job Poster Builder.
291 1
     *
292
     * @param  \Illuminate\Http\Request $request   Incoming request object.
293 1
     * @param  \App\Models\JobPoster    $jobPoster Job Poster object.
294 1
     * @return \Illuminate\Http\Response
295
     */
296 1
    public function edit(Request $request, JobPoster $jobPoster)
297 1
    {
298 1
        $manager = $jobPoster->manager;
299 1
300
        $defaultQuestionManager = new JobPosterDefaultQuestions();
301
        $defaultQuestionManager->initializeQuestionsIfEmpty($jobPoster);
302 1
303
        return view(
0 ignored issues
show
Bug introduced by
The function view 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

303
        return /** @scrutinizer ignore-call */ view(
Loading history...
304 1
            'manager/job_create',
305
            [
306 1
                // Localization Strings.
307
                'job_l10n' => Lang::get('manager/job_edit'),
308 1
                // Data.
309 1
                'manager' => $manager,
310
                'job' => $jobPoster,
311 1
                'form_action_url' => route('admin.jobs.update', $jobPoster),
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

311
                'form_action_url' => /** @scrutinizer ignore-call */ route('admin.jobs.update', $jobPoster),
Loading history...
312 1
            ]
313 1
        );
314 1
    }
315
316
    /**
317 1
     * Create a blank job poster for the specified manager
318
     *
319 1
     * @param  \App\Models\Manager $manager Incoming Manager object.
320
     * @return \Illuminate\Http\Response Job Create view
321 1
     */
322
    public function createAsManager(Manager $manager)
323 1
    {
324 1
        $jobPoster = new JobPoster();
325
        $jobPoster->manager_id = $manager->id;
326
327
        // Save manager-specific info to the job poster - equivalent to the intro step of the JPB
328 1
        $divisionEn = $manager->getTranslation('division', 'en');
329 1
        $divisionFr = $manager->getTranslation('division', 'fr');
330
        $jobPoster->fill([
331
            'department_id' => $manager->user->department_id,
332 1
            'division' => ['en' => $divisionEn],
333 1
            'division' => ['fr' => $divisionFr],
334
        ]);
335
336
        $jobPoster->save();
337 1
338
        return redirect()->route('manager.jobs.edit', $jobPoster->id);
0 ignored issues
show
Bug introduced by
The function redirect 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

338
        return /** @scrutinizer ignore-call */ redirect()->route('manager.jobs.edit', $jobPoster->id);
Loading history...
339 1
    }
340
341 1
    /**
342
     * Update a resource in storage
343 1
     * NOTE: Only saves fields that are not on the react-built Job Poster Builder
344 1
     *
345 1
     * @param  \Illuminate\Http\Request $request   Incoming request object.
346
     * @param  \App\Models\JobPoster    $jobPoster Optional Job Poster object.
347 1
     * @return \Illuminate\Http\Response
348
     */
349 1
    public function store(Request $request, JobPoster $jobPoster)
350 1
    {
351 1
        // Don't allow edits for published Job Posters
352
        // Also check auth while we're at it.
353 1
        $this->authorize('update', $jobPoster);
354 1
        JobPosterValidator::validateUnpublished($jobPoster);
355
356
        $input = $request->input();
357 1
358
        if ($jobPoster->manager_id == null) {
359 1
            $jobPoster->manager_id = $request->user()->manager->id;
360 1
            $jobPoster->save();
361 1
        }
362 1
363 1
        if ($request->input('question')) {
364 1
            $validator = Validator::make($request->input('question'), [
365 1
                '*.question.*' => 'required|string',
366 1
            ], [
367 1
                'required' => Lang::get('validation.custom.job_poster_question.required'),
368 1
                'string' => Lang::get('validation.custom.job_poster_question.string')
369 1
            ]);
370
371
            if ($validator->fails()) {
372
                $request->session()->flash('errors', $validator->errors());
373
                return redirect(route('admin.jobs.edit', $jobPoster->id));
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

373
                return redirect(/** @scrutinizer ignore-call */ route('admin.jobs.edit', $jobPoster->id));
Loading history...
Bug introduced by
The function redirect 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

373
                return /** @scrutinizer ignore-call */ redirect(route('admin.jobs.edit', $jobPoster->id));
Loading history...
374
            }
375
        }
376
377
        $this->fillAndSaveJobPosterQuestions($input, $jobPoster, true);
378
379
        return redirect(route('manager.jobs.preview', $jobPoster->id));
380
    }
381 6
382
    /**
383
     * Fill Job Poster's questions and save
384
     *
385 6
     * @param  mixed[]               $input     Field values.
386 6
     * @param  \App\Models\JobPoster $jobPoster Job Poster object.
387 6
     * @param  boolean               $replace   Remove existing relationships.
388
     * @return void
389
     */
390
    protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace): void
391
    {
392 6
        if ($replace) {
393
            $jobPoster->job_poster_questions()->delete();
394 6
        }
395
396 6
        if (!array_key_exists('question', $input) || !is_array($input['question'])) {
397
            return;
398
        }
399
400
        foreach ($input['question'] as $question) {
401 6
            $jobQuestion = new JobPosterQuestion();
402
            $jobQuestion->job_poster_id = $jobPoster->id;
403 6
            $jobQuestion->fill(
404
                [
405 6
                    'question' => [
406
                        'en' => $question['question']['en'],
407 6
                        'fr' => $question['question']['fr']
408
409 6
                    ],
410
                    'description' => [
411
                        'en' => $question['description']['en'],
412
                        'fr' => $question['description']['fr']
413
                    ]
414
                ]
415
            );
416
            $jobPoster->save();
417
            $jobQuestion->save();
418
        }
419 6
    }
420
421 6
    /**
422
     * Downloads a CSV file with the applicants who have applied to the job poster.
423 6
     *
424 6
     * @param  \App\Models\JobPoster $jobPoster Job Poster object.
425 6
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
426 6
     */
427 6
    protected function downloadApplicants(JobPoster $jobPoster)
428 6
    {
429 6
        $tables = [];
430 6
        // The first row in the array represents the names of the columns in the spreadsheet.
431 6
        $tables[0] = ['Status', 'Applicant Name', 'Email', 'Language'];
432 6
433 6
        $application_status_id = ApplicationStatus::where('name', 'submitted')->first()->id;
434 6
        $applications = JobApplication::where('job_poster_id', $jobPoster->id)
435 6
            ->where('application_status_id', $application_status_id)
436 6
            ->get();
437
438 6
        $index = 1;
439 6
        foreach ($applications as $application) {
440 6
            $status = '';
441 6
            $username = $application->user_name;
442 6
            $user_email = $application->user_email;
443 6
            $language = strtoupper($application->preferred_language->name);
444
            // If the applicants veteran status name is NOT 'none' then set status to veteran.
445
            $non_veteran = VeteranStatus::where('name', 'none')->first()->id;
446 6
            if ($application->veteran_status_id != $non_veteran) {
447 6
                $status = 'Veteran';
448 6
            } else {
449 6
                // Check if the applicant is a canadian citizen.
450 6
                $canadian_citizen = CitizenshipDeclaration::where('name', 'citizen')->first()->id;
451 6
                if ($application->citizenship_declaration->id == $canadian_citizen) {
452
                    $status = 'Citizen';
453
                } else {
454
                    $status = 'Non-citizen';
455 6
                }
456 6
            }
457
            $tables[$index] = [$status, $username, $user_email, $language];
458
            $index++;
459
        }
460
461
        $filename = $jobPoster->id . '-' . 'applicants-data.csv';
462
463
        // Open file.
464
        $file = fopen($filename, 'w');
465
        // Iterate through tables and add each line to csv file.
466 6
        foreach ($tables as $line) {
467
            fputcsv($file, $line);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

467
            fputcsv(/** @scrutinizer ignore-type */ $file, $line);
Loading history...
468 6
        }
469 6
        // Close open file.
470
        fclose($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

470
        fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
471
472 6
        $headers = [
473 6
            'Content-Type' => 'text/csv',
474
            'Content-Disposition' => 'attachment; filename=' . $filename,
475
        ];
476
477
        return Response::download($filename, $filename, $headers);
478
    }
479
}
480