Completed
Pull Request — master (#8)
by Karl
18:46 queued 16:09
created

CollectJobsForUser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 5
    public function __construct(User $user)
41
    {
42 5
        $this->user = $user;
43 5
    }
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 5
    public function handle(JobsMulti $jobsClient)
53
    {
54
        // Collect jobs based on the user's keyword and location
55 5
        $jobsByProvider = $jobsClient->setKeyword($this->user->keyword)
56 5
            ->setLocation($this->user->location)
57 5
            ->setPage(1, self::MAX_JOBS_FROM_PROVIDER)
58 5
            ->getAllJobs();
59
60
        // Sort jobs into one array
61 5
        $jobs = $this->getJobsFromCollections($jobsByProvider);
62 5
        $jobs = $this->sortJobs($jobs);
63
64
        // Trigger notification to user
65 5
        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...
66 4
            $this->user->notify(new JobsCollected($jobs));
67
        } else {
68 1
            Log::info("No jobs found for user {$this->user->id}");
69
        }
70 5
        return $jobs;
71
    }
72
73
    /**
74
     * Sort jobs by date posted, desc
75
     *
76
     * @param array $jobs
77
     *
78
     * @return array
79
     */
80 5
    protected function sortJobs($jobs = [])
81
    {
82
        // Sort by date
83
        usort($jobs, function ($item1, $item2) {
84 4
            return $item2->datePosted <=> $item1->datePosted;
85 5
        });
86
        // Filter any older than max age
87
        $jobs = array_filter($jobs, function ($job) {
88 4
            return $job->datePosted > new \DateTime(self::MAX_DAYS_OLD.' days ago');
89 5
        });
90
        // Truncate to the max number of results
91 5
        return array_slice($jobs, 0, self::MAX_JOBS);
92
    }
93
94
    /**
95
     * Convert the array of collections to one large array
96
     *
97
     * @param array $collectionsArray
98
     *
99
     * @return array
100
     */
101 5
    protected function getJobsFromCollections($collectionsArray = [])
102
    {
103 5
        $jobs = [];
104 5
        array_walk_recursive(
105
            $collectionsArray,
106 5
            function (Collection $collection) use (&$jobs) {
107 5
                $jobListings = array_slice(
108 5
                    $collection->all(),
109 5
                    0,
110 5
                    self::MAX_JOBS_FROM_PROVIDER
111
                );
112 5
                foreach ($jobListings as $jobListing) {
113 4
                    $jobs[] = $jobListing;
114
                }
115 5
            }
116
        );
117 5
        return $jobs;
118
    }
119
}
120