1
|
|
|
<?php namespace JobApis\JobsToMail\Jobs; |
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\Collection; |
9
|
|
|
use JobApis\Jobs\Client\JobsMulti; |
10
|
|
|
use JobApis\JobsToMail\Models\User; |
11
|
|
|
use JobApis\JobsToMail\Notifications\JobsCollected; |
12
|
|
|
|
13
|
|
|
class CollectJobsForUser implements ShouldQueue |
14
|
|
|
{ |
15
|
|
|
use InteractsWithQueue, Queueable, SerializesModels; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var User user to send jobs to. |
19
|
|
|
*/ |
20
|
|
|
protected $user; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The maximum number of jobs to return |
24
|
|
|
*/ |
25
|
|
|
const MAX_JOBS = 50; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The maximum number of jobs from each provider |
29
|
|
|
*/ |
30
|
|
|
const MAX_JOBS_FROM_PROVIDER = 10; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The maximum age of a job to be included |
34
|
|
|
*/ |
35
|
|
|
const MAX_DAYS_OLD = 14; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new job instance. |
39
|
|
|
*/ |
40
|
6 |
|
public function __construct(User $user) |
41
|
|
|
{ |
42
|
6 |
|
$this->user = $user; |
43
|
6 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Collect and sort jobs from multiple APIs using the JobsMulti client. |
47
|
|
|
* |
48
|
|
|
* @param JobsMulti $jobsClient |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
6 |
|
public function handle(JobsMulti $jobsClient) |
53
|
|
|
{ |
54
|
|
|
// Collect jobs based on the user's keyword and location |
55
|
6 |
|
$jobsByProvider = $jobsClient->setKeyword($this->user->keyword) |
56
|
6 |
|
->setLocation($this->user->location) |
57
|
6 |
|
->setPage(1, self::MAX_JOBS_FROM_PROVIDER) |
58
|
6 |
|
->getAllJobs(); |
59
|
|
|
|
60
|
|
|
// Sort jobs into one array |
61
|
6 |
|
$jobs = $this->getJobsFromCollections($jobsByProvider); |
62
|
6 |
|
$jobs = $this->sortJobs($jobs); |
63
|
|
|
|
64
|
|
|
// Trigger notification to user |
65
|
6 |
|
if ($jobs) { |
|
|
|
|
66
|
5 |
|
$this->user->notify(new JobsCollected($jobs)); |
67
|
|
|
} else { |
68
|
1 |
|
Log::info("No jobs found for user {$this->user->id}"); |
69
|
|
|
} |
70
|
6 |
|
return $jobs; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Convert the array of collections to one large array |
75
|
|
|
* |
76
|
|
|
* @param array $collectionsArray |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
6 |
|
protected function getJobsFromCollections($collectionsArray = []) |
81
|
|
|
{ |
82
|
6 |
|
$jobs = []; |
83
|
6 |
|
array_walk_recursive( |
84
|
|
|
$collectionsArray, |
85
|
|
|
function (Collection $collection) use (&$jobs) { |
86
|
6 |
|
$this->logErrorsFromCollection($collection); |
87
|
6 |
|
$jobListings = array_slice( |
88
|
6 |
|
$collection->all(), |
89
|
6 |
|
0, |
90
|
6 |
|
self::MAX_JOBS_FROM_PROVIDER |
91
|
|
|
); |
92
|
6 |
|
foreach ($jobListings as $jobListing) { |
93
|
5 |
|
$jobs[] = $jobListing; |
94
|
|
|
} |
95
|
6 |
|
} |
96
|
|
|
); |
97
|
6 |
|
return $jobs; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Logs all the errors attached to a collection |
102
|
|
|
* |
103
|
|
|
* @param array $jobsByProvider |
|
|
|
|
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
6 |
|
protected function logErrorsFromCollection(Collection $collection) |
108
|
|
|
{ |
109
|
6 |
|
if ($collection->getErrors()) { |
110
|
1 |
|
foreach ($collection->getErrors() as $error) { |
111
|
1 |
|
Log::error($error); |
112
|
|
|
} |
113
|
|
|
} |
114
|
6 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Sort jobs by date posted, desc |
118
|
|
|
* |
119
|
|
|
* @param array $jobs |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
6 |
|
protected function sortJobs($jobs = []) |
124
|
|
|
{ |
125
|
|
|
// Sort by date |
126
|
|
|
usort($jobs, function ($item1, $item2) { |
127
|
5 |
|
return $item2->datePosted <=> $item1->datePosted; |
128
|
6 |
|
}); |
129
|
|
|
// Filter any older than max age |
130
|
6 |
|
$jobs = array_filter($jobs, function ($job) { |
131
|
5 |
|
return $job->datePosted > new \DateTime(self::MAX_DAYS_OLD.' days ago'); |
132
|
6 |
|
}); |
133
|
|
|
// Truncate to the max number of results |
134
|
6 |
|
return array_slice($jobs, 0, self::MAX_JOBS); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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.