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

ScreenCandidatesPrompt::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 55
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 55
rs 9.328
cc 2
nc 2
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\Mail;
4
5
use App\Models\JobPoster;
6
use App\Models\Lookup\CitizenshipDeclaration;
7
use App\Models\Lookup\VeteranStatus;
8
use App\Models\User;
9
use Illuminate\Bus\Queueable;
10
use Illuminate\Mail\Mailable;
11
use Illuminate\Queue\SerializesModels;
12
use Illuminate\Contracts\Queue\ShouldQueue;
13
use Illuminate\Support\Facades\App;
14
use Illuminate\Support\Facades\Config;
15
use Illuminate\Support\Facades\Lang;
16
use Illuminate\Support\Facades\Log;
17
use Jenssegers\Date\Date;
18
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
19
20
class ScreenCandidatesPrompt extends Mailable implements ShouldQueue
21
{
22
    use Queueable, SerializesModels;
23
24
    /**
25
     * The Job Poster instance.
26
     *
27
     * @var JobPoster
28
     */
29
    public $job;
30
31
    /**
32
     * Create a new message instance.
33
     *
34
     * @return void
35
     */
36
    public function __construct(JobPoster $job)
37
    {
38
        $this->job = $job;
39
    }
40
41
    /**
42
     * Build the message.
43
     *
44
     * @return $this
45
     */
46
    public function build()
47
    {
48
        // Get emails of all Hr Advisors who have claimed job.
49
        $hr_advisors_emails = [];
50
        $hr_advisors = $this->job->hr_advisors;
51
52
        foreach ($hr_advisors as $advisor) {
53
            $email = User::where('id', $advisor->user_id)->first()->email;
54
            array_push($hr_advisors_emails, $email);
55
        }
56
57
        // Get date 2 weeks after job poster closes.
58
        $date_in_two_weeks = humanizeLastDay($this->job->close_date_time->addWeeks(2));
59
        $date_in_two_weeks_fr = humanizeLastDay($this->job->close_date_time->addWeeks(2), 'fr');
60
61
        // Number of applicants.
62
        $num_of_applicants = $this->job->job_applications->count();
63
64
        // Number of non-citizens.
65
        $canadian_citizen = CitizenshipDeclaration::where('name', 'citizen')->first()->id;
66
        $num_of_noncitizens = $this->job->job_applications
67
                                ->where('citizenship_declaration_id', '!=', $canadian_citizen)
68
                                ->count();
69
70
        // Number of veterans.
71
        $non_veteran = VeteranStatus::where('name', 'none')->first()->id;
72
        $num_of_veterans = $this->job->job_applications
73
                            ->where('veteran_status_id', '!=', $non_veteran)
74
                            ->count();
75
76
        $position = $this->job->getTranslations('title');
77
        $classification = $this->job->getClassificationMessageAttribute();
78
        $subject = Lang::get('common/notifications/screen_candidates.subject', [ 'position' => $position['en'], 'classification' => $classification ]) . ' / ' . Lang::get('common/notifications/screen_candidates.subject', [ 'position' => $position['fr'], 'classification' => $classification ], 'fr');
79
80
        return $this->subject($subject)
81
                    ->cc($hr_advisors_emails)
82
                    ->cc(config('mail.admin_address'))
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

82
                    ->cc(/** @scrutinizer ignore-call */ config('mail.admin_address'))
Loading history...
83
                    ->markdown('emails.job_posters.screen_candidates_plain', [
84
                        'drop_off_date' => [
85
                            'en' => $date_in_two_weeks,
86
                            'fr' => $date_in_two_weeks_fr,
87
                        ],
88
                        'manager_portal_link' => [
89
                            '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

89
                            'en' => /** @scrutinizer ignore-call */ route('manager.home'),
Loading history...
90
                            'fr' => LaravelLocalization::getLocalizedURL('fr', route('manager.home')),
91
                        ],
92
                        'num_of_applicants' => $num_of_applicants,
93
                        'num_of_noncitizens' => $num_of_noncitizens,
94
                        'num_of_veterans' => $num_of_veterans,
95
                        'position' => $position,
96
                        'position_link' => [
97
                            'en' => route('jobs.summary', $this->job->id),
98
                            'fr' => LaravelLocalization::getLocalizedURL('fr', route('jobs.summary', $this->job->id)),
99
                        ],
100
                        'talent_cloud_email' => config('mail.admin_address'),
101
                    ]);
102
    }
103
}
104