Passed
Push — feature/screen-candidates-emai... ( 13d7df )
by Yonathan
07:07 queued 02:02
created

SendScreenCandidatesEmail   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 41
c 1
b 0
f 0
dl 0
loc 87
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A screenCandidatesPrompt() 0 57 3
A __construct() 0 2 1
A handle() 0 5 1
1
<?php
2
3
namespace App\Listeners;
4
5
use App\Events\JobSaved;
6
use App\Mail\ScreenCandidatesPrompt;
7
use App\Models\HrAdvisor;
8
use App\Models\Lookup\CitizenshipDeclaration;
9
use App\Models\Lookup\VeteranStatus;
10
use App\Models\User;
11
use Illuminate\Queue\InteractsWithQueue;
12
use Illuminate\Contracts\Queue\ShouldQueue;
13
use Illuminate\Support\Facades\App;
14
use Illuminate\Support\Facades\Config;
15
use Illuminate\Support\Facades\Log;
16
use Illuminate\Support\Facades\Mail;
17
18
class SendScreenCandidatesEmail implements ShouldQueue
19
{
20
    /**
21
     * Create the event listener.
22
     *
23
     * @return void
24
     */
25
    public function __construct()
26
    {
27
    }
28
29
    /**
30
     * Sends email to managers, prompting to start screening applicants.
31
     *
32
     * @return void
33
     */
34
    public function screenCandidatesPrompt($job) // phpcs:ignore
35
    {
36
        // Get emails of all Hr Advisors who have claimed job
37
        $hr_advisors_emails = [];
38
        $hr_advisors = $job->hr_advisors;
39
40
        foreach ($hr_advisors as $advisor) {
41
            $email = User::where('id', $advisor->user_id)->first()->email;
42
            array_push($hr_advisors_emails, $email);
43
        }
44
45
        // Get date 2 weeks after job poster closes
46
        $dateFormat = Config::get('app.date_format');
47
        $locale = App::getLocale();
48
        $timestamp_in_two_weeks = strtotime('+2 weeks', $job->close_date_time->timestamp);
49
        $date_in_two_weeks = date($dateFormat[$locale], $timestamp_in_two_weeks);
50
51
        // Number of applicants
52
        $num_of_applicants = $job->job_applications->count();
53
54
        // Number of non-citizens
55
        $canadian_citizen = CitizenshipDeclaration::where('name', 'citizen')->first()->id;
56
        $num_of_noncitizens = $job->job_applications
57
                                ->where('citizenship_declaration_id', '!=', $canadian_citizen)
58
                                ->count();
59
60
        // Number of veterans
61
        $non_veteran = VeteranStatus::where('name', 'none')->first()->id;
62
        $num_of_veterans = $job->job_applications
63
                            ->where('veteran_status_id', '!=', $non_veteran)
64
                            ->count();
65
66
        // Create array of data needed for ScreeningCandidatesPrompt mailable
67
        $mailData = [
68
            'classification' => $job->getClassificationMessageAttribute(),
69
            'drop_off_date' => $date_in_two_weeks,
70
            'hr_advisors_emails' => $hr_advisors_emails,
71
            'manager_portal_link' => [
72
                'en' => route('manager.home'),
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

72
                'en' => /** @scrutinizer ignore-call */ route('manager.home'),
Loading history...
73
                'fr' => route('manager.home'),
74
            ],
75
            'num_of_applicants' => $num_of_applicants,
76
            'num_of_noncitizens' => $num_of_noncitizens,
77
            'num_of_veterans' => $num_of_veterans,
78
            'position' => $job->getTranslations('title'),
79
            'position_link' => [
80
                'en' => route('jobs.show', $job),
81
                'fr' => route('jobs.show', $job),
82
            ],
83
            'talent_cloud_email' => '[email protected]',
84
        ];
85
86
        $manager_email = $job->manager->user->email;
87
        if (isset($manager_email)) {
88
            Mail::to($manager_email)->send(new ScreenCandidatesPrompt($mailData));
89
        } else {
90
            Log::error('The screen applicants email to manager has not been set.');
91
        }
92
    }
93
94
    /**
95
     * Handle the event.
96
     *
97
     * @param  JobSaved  $event
98
     * @return void
99
     */
100
    public function handle(JobSaved $event)
101
    {
102
        $job = $event->job;
103
104
        $this->screenCandidatesPrompt($job);
105
    }
106
}
107