Api::search()   B
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 22
ccs 14
cts 14
cp 1
rs 8.6737
cc 6
eloc 14
nc 32
nop 1
crap 6
1
<?php
2
3
namespace Jobles\Careerjet;
4
5
use Jobles\Careerjet\Builder\JobBuilder;
6
use Jobles\Core\API\SearchInterface;
7
use Jobles\Core\Job\JobCollection;
8
9
class Api implements SearchInterface
10
{
11
12
    /**
13
     * @var string
14
     */
15
    private $affiliateId;
16
17
    /**
18
     * @var \Careerjet_API
19
     */
20
    private $api;
21
22
    /**
23
     * @var string
24
     */
25
    private $country;
26
27
    /**
28
     * @var string
29
     */
30
    private $language;
31
32 1
    public function __construct(
33
        string $affiliateId,
34
        string $country,
35
        string $language = null,
36
        \Careerjet_API $api = null
37
    ) {
38 1
        $this->affiliateId = $affiliateId;
39 1
        $this->country = $country;
40 1
        $this->language = $language;
41
42 1
        $this->api = !empty($api) ? $api : new \Careerjet_API(Locale::byCountryAndLanguage($country, $language));
43 1
    }
44
45
    /**
46
     * @param array $filters
47
     *
48
     * @return JobCollection
49
     * @throws Exception\CareerjetException
50
     */
51 1
    public function search(array $filters = []) : JobCollection
52
    {
53 1
        $collection = new JobCollection;
54 1
        $search = ['affid' => $this->affiliateId];
55 1
        if (isset($filters['keywords'])) { // empty allowed
56 1
            $search['keywords'] = $filters['keywords'];
57
        }
58 1
        if (isset($filters['location'])) { // empty allowed
59 1
            $search['location'] = $filters['location'];
60
        }
61 1
        if (isset($filters['limit'])) { // max 99
62 1
            $search['pagesize'] = $filters['limit'];
63
        }
64 1
        $search['page'] = isset($filters['offset']) ? $filters['offset'] : 1; // starts on 1
65
66 1
        $result = $this->api->search($search);
67 1
        foreach ($result->jobs as $job) {
68 1
            $collection->add(JobBuilder::fromApi($job, $this->country));
69
        }
70
71 1
        return $collection;
72
    }
73
}
74