Completed
Push — master ( e48405...3cf851 )
by Karl
27s
created

SearchAndNotifyUser::handle()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 12
cts 12
cp 1
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 17
nc 2
nop 4
crap 2
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\JobsMulti;
9
use JobApis\JobsToMail\Filters\CollectionFilter;
10
use JobApis\JobsToMail\Filters\JobFilter;
11
use JobApis\JobsToMail\Filters\RecruiterFilter;
12
use JobApis\JobsToMail\Models\Recruiter;
13
use JobApis\JobsToMail\Models\Search;
14
use JobApis\JobsToMail\Notifications\JobsCollected;
15
16
class SearchAndNotifyUser implements ShouldQueue
17
{
18
    use InteractsWithQueue, Queueable, SerializesModels;
19
20
    /**
21
     * @var array Jobs collected from providers.
22
     */
23
    protected $jobs = [];
24
25
    /**
26
     * @var Recruiter recruiter model
27
     */
28
    protected $recruiter;
29
30
    /**
31
     * @var Search search to conduct
32
     */
33
    protected $search;
34
35
    /**
36
     * The maximum number of jobs to return
37
     */
38
    const MAX_JOBS = 50;
39
40
    /**
41
     * The maximum number of jobs from each provider
42
     */
43
    const MAX_JOBS_FROM_PROVIDER = 10;
44
45
    /**
46
     * The maximum age of a job to be included
47
     */
48
    const MAX_DAYS_OLD = 14;
49
50
    /**
51
     * Create a new job instance.
52
     */
53 3
    public function __construct(Search $search)
54
    {
55 3
        $this->search = $search;
56 3
    }
57
58
    /**
59
     * Collect and sort jobs from multiple APIs using the JobsMulti client.
60
     *
61
     * @param JobsMulti $jobsClient
62
     * @param CollectionFilter $collectionFilter
63
     * @param JobFilter $jobFilter
64
     * @param RecruiterFilter $recruiterFilter
65
     *
66
     * @return array
67
     */
68 3
    public function handle(
69
        JobsMulti $jobsClient,
70
        CollectionFilter $collectionFilter,
71
        JobFilter $jobFilter,
72
        RecruiterFilter $recruiterFilter
73
    ) {
74
        // Collect jobs based on the Search keyword and location
75 3
        $jobsByProvider = $jobsClient->setKeyword($this->search->keyword)
76 3
            ->setLocation($this->search->location)
77 3
            ->setPage(1, self::MAX_JOBS_FROM_PROVIDER)
78 3
            ->getAllJobs();
79
80
        // Get jobs Array from array of Collections
81 3
        $jobs = $collectionFilter->getJobsFromCollections($jobsByProvider, self::MAX_JOBS_FROM_PROVIDER);
82
83
        // Sort the jobs
84 3
        $jobs = $jobFilter->sort($jobs, self::MAX_DAYS_OLD, self::MAX_JOBS);
85
86
        // Filter jobs from recruiters
87 3
        $jobs = $recruiterFilter->filter($jobs, $this->search);
88
89
        // Trigger notification to user
90 3
        if ($jobs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $jobs of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
91 2
            $this->search->user->notify(new JobsCollected($jobs, $this->search));
92
        } else {
93 1
            Log::info("No jobs found for search {$this->search->id}");
94
        }
95 3
        return $jobs;
96
    }
97
}
98