1
|
|
|
<?php namespace JobApis\JobsToMail\Jobs\Notifications; |
2
|
|
|
|
3
|
|
|
use Illuminate\Bus\Queueable; |
4
|
|
|
use Illuminate\Queue\SerializesModels; |
5
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
6
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
7
|
|
|
use Illuminate\Support\Facades\Log; |
8
|
|
|
use JobApis\Jobs\Client\JobsMulti; |
9
|
|
|
use JobApis\JobsToMail\Filters\RecruiterFilter; |
10
|
|
|
use JobApis\JobsToMail\Models\Recruiter; |
11
|
|
|
use JobApis\JobsToMail\Models\Search; |
12
|
|
|
use JobApis\JobsToMail\Notifications\JobsCollected; |
13
|
|
|
|
14
|
|
|
class SearchAndNotifyUser implements ShouldQueue |
15
|
|
|
{ |
16
|
|
|
use InteractsWithQueue, Queueable, SerializesModels; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array Jobs collected from providers. |
20
|
|
|
*/ |
21
|
|
|
protected $jobs = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Recruiter recruiter model |
25
|
|
|
*/ |
26
|
|
|
protected $recruiter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Search search to conduct |
30
|
|
|
*/ |
31
|
|
|
protected $search; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The maximum number of jobs to return |
35
|
|
|
*/ |
36
|
|
|
const MAX_JOBS = 25; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The maximum number of jobs from each provider |
40
|
|
|
*/ |
41
|
|
|
const MAX_JOBS_FROM_PROVIDER = 10; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The maximum age of a job to be included |
45
|
|
|
*/ |
46
|
|
|
const MAX_DAYS_OLD = 2; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a new job instance. |
50
|
|
|
*/ |
51
|
2 |
|
public function __construct(Search $search) |
52
|
|
|
{ |
53
|
2 |
|
$this->search = $search; |
54
|
2 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Collect and sort jobs from multiple APIs using the JobsMulti client. |
58
|
|
|
* |
59
|
|
|
* @param JobsMulti $jobsClient |
60
|
|
|
* @param RecruiterFilter $recruiterFilter |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
2 |
|
public function handle( |
65
|
|
|
JobsMulti $jobsClient, |
66
|
|
|
RecruiterFilter $recruiterFilter |
67
|
|
|
) { |
68
|
|
|
// Add options for max results and age |
69
|
|
|
$options = [ |
70
|
2 |
|
'maxAge' => self::MAX_DAYS_OLD, |
71
|
2 |
|
'maxResults' => self::MAX_JOBS, |
72
|
2 |
|
'orderBy' => 'datePosted', |
73
|
2 |
|
'order' => 'desc', |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
// Collect jobs based on the Search keyword and location |
77
|
2 |
|
$jobs = $jobsClient->setKeyword($this->search->keyword) |
78
|
2 |
|
->setLocation($this->search->location) |
79
|
2 |
|
->setPage(1, self::MAX_JOBS_FROM_PROVIDER) |
80
|
2 |
|
->getAllJobs($options); |
81
|
|
|
|
82
|
|
|
// Filter jobs from recruiters and convert to array |
83
|
2 |
|
$jobs = $recruiterFilter->filter($jobs->all(), $this->search); |
84
|
|
|
|
85
|
|
|
// Trigger notification to user |
86
|
2 |
|
if ($jobs) { |
|
|
|
|
87
|
1 |
|
$this->search->user->notify(new JobsCollected($jobs, $this->search)); |
88
|
|
|
} else { |
89
|
1 |
|
Log::info("No jobs found for search {$this->search->id}"); |
90
|
|
|
} |
91
|
2 |
|
return $jobs; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.