Passed
Push — task/experience-education ( 788037...f8bedd )
by Tristan
05:20
created

ProcessJobStatusTransitions   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 19
c 2
b 0
f 0
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A handle() 0 27 3
1
<?php
2
3
namespace App\Jobs;
4
5
use Jenssegers\Date\Date;
6
use App\Models\JobPoster;
7
use App\Models\Lookup\JobPosterStatus;
8
use Illuminate\Bus\Queueable;
9
use Illuminate\Contracts\Queue\ShouldQueue;
10
use Illuminate\Foundation\Bus\Dispatchable;
11
use Illuminate\Queue\InteractsWithQueue;
12
use Illuminate\Queue\SerializesModels;
13
use Illuminate\Support\Facades\Log;
14
15
class ProcessJobStatusTransitions implements ShouldQueue
16
{
17
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
18
19
    /**
20
     * Create a new job instance.
21
     *
22
     * @return void
23
     */
24
    public function __construct()
25
    {
26
        //
27
    }
28
29
    /**
30
     * Execute the job.
31
     *
32
     * @return void
33
     */
34
    public function handle()
35
    {
36
        Log::debug('Processing job status transitions');
37
38
        $now = Date::now();
39
        $ready = JobPosterStatus::where('key', 'ready')->first();
40
        $live = JobPosterStatus::where('key', 'live')->first();
41
        $assessment = JobPosterStatus::where('key', 'assessment')->first();
42
43
        $jobsReadyForLive = JobPoster::where('job_poster_status_id', $ready->id)
44
            ->where('open_date_time', '<=', $now)->get();
45
46
        Log::debug('Jobs ready for live:');
47
        Log::debug($jobsReadyForLive);
48
49
        // We want to call save on each model individually instead of doing a mass update in order to trigger
50
        // any events that may be listening for eloquent model udpates.
51
        foreach ($jobsReadyForLive as $job) {
52
            $job->job_poster_status_id = $live->id;
53
            $job->save();
54
        }
55
56
        $jobsReadyForAssessment = JobPoster::where('job_poster_status_id', $live->id)
57
            ->where('close_date_time', '<=', $now)->get();
58
        foreach ($jobsReadyForAssessment as $job) {
59
            $job->job_poster_status_id = $assessment->id;
60
            $job->save();
61
        }
62
    }
63
}
64