Completed
Push — master ( f95505...a1ea25 )
by Karl
09:22
created

CareercastProvider::setValidThroughAsString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php namespace JobApis\Jobs\Client\Providers;
2
3
use JobApis\Jobs\Client\Job;
4
5
class CareercastProvider extends AbstractProvider
6
{
7
    /**
8
     * Returns the standardized job object
9
     *
10
     * @param array $payload
11
     *
12
     * @return \JobApis\Jobs\Client\Job
13
     */
14 4
    public function createJobObject($payload)
15
    {
16 4
        $job = new Job([
17 4
            'description' => $payload['Description'],
18 4
            'title' => $payload['JobTitle'],
19 4
            'name' => $payload['JobTitle'],
20 4
            'url' => $payload['Url'],
21 4
            'sourceId' => $payload['Id'],
22 4
            'qualifications' => $payload['Requirements'],
23 4
            'maximumSalaray' => $payload['SalaryMax'],
24 4
            'minimumSalaray' => $payload['SalaryMin'],
25 4
            'baseSalaray' => $payload['SalaryMin'],
26 4
        ]);
27 4
        $job->setDatePostedAsString($payload['PostDate']);
28 4
        $job = $this->setValidThroughAsString($payload['ExpireDate'], $job);
29 4
        $job = $this->setCategory($payload, $job);
30 4
        $job = $this->setCompany($payload, $job);
31 4
        return $this->setLocation($payload, $job);
32
    }
33
34
    /**
35
     * Job response object default keys that should be set
36
     *
37
     * @return  string
38
     */
39 4
    public function getDefaultResponseFields()
40
    {
41
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('Descriptio..., 'WorkStatusDisplay'); (string[]) is incompatible with the return type declared by the abstract method JobApis\Jobs\Client\Prov...etDefaultResponseFields of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
42 4
            'Description',
43 4
            'JobTitle',
44 4
            'Url',
45 4
            'Id',
46 4
            'PostDate',
47 4
            'ExpireDate',
48 4
            'Requirements',
49 4
            'SalaryMax',
50 4
            'SalaryMin',
51 4
            'SalaryMin',
52 4
            'CategoryDisplay',
53 4
            'WorkStatusDisplay',
54 4
        ];
55
    }
56
57
    /**
58
     * Get listings path
59
     *
60
     * @return  string
61
     */
62 4
    public function getListingsPath()
63
    {
64 4
        return 'Jobs';
65
    }
66
67
    /**
68
     * Parses the category and work type and attaches it to the job
69
     *
70
     * @param $payload array
71
     * @param $job \JobApis\Jobs\Client\Job
72
     *
73
     * @return \JobApis\Jobs\Client\Job
74
     */
75 4
    protected function setCategory($payload, $job)
76
    {
77 4
        if (isset($payload['CategoryDisplay']) && is_array($payload['CategoryDisplay'])) {
78 4
            $job->setOccupationalCategory(implode(', ', $payload['CategoryDisplay']));
79 4
        }
80 4
        if (isset($payload['CategoryDisplay']) && is_array($payload['WorkStatusDisplay'])) {
81 4
            $job->setEmploymentType(implode(', ', $payload['WorkStatusDisplay']));
82 4
        }
83 4
        return $job;
84
    }
85
86
    /**
87
     * Parses the company name and attaches it to the job
88
     *
89
     * @param $payload array
90
     * @param $job \JobApis\Jobs\Client\Job
91
     *
92
     * @return \JobApis\Jobs\Client\Job
93
     */
94 4
    protected function setCompany($payload, $job)
95
    {
96 4
        if (isset($payload['Company'])) {
97 4
            $job->setCompany($payload['Company']);
98 4
        }
99 4
        return $job;
100
    }
101
102
    /**
103
     * Parses the location and attaches it to the job
104
     *
105
     * @param $payload array
106
     * @param $job \JobApis\Jobs\Client\Job
107
     *
108
     * @return \JobApis\Jobs\Client\Job
109
     */
110 4
    protected function setLocation($payload, $job)
111
    {
112 4
        if (isset($payload['FormattedCityState'])) {
113 4
            $job->setLocation($payload['FormattedCityState']);
114 4
        }
115 4
        if (isset($payload['State'])) {
116 4
            $job->setState($payload['State']);
117 4
        }
118 4
        if (isset($payload['Country'])) {
119 4
            $job->setCountry($payload['Country']);
120 4
        }
121 4
        if (isset($payload['City'])) {
122 4
            $job->setCity($payload['City']);
123 4
        }
124 4
        if (isset($payload['Longitude'])) {
125 4
            $job->setLongitude($payload['Longitude']);
126 4
        }
127 4
        if (isset($payload['Latitude'])) {
128 4
            $job->setLatitude($payload['Latitude']);
129 4
        }
130 4
        if (isset($payload['Zip'])) {
131 4
            $job->setPostalCode($payload['Zip']);
132 4
        }
133 4
        return $job;
134
    }
135
136
    /**
137
     * Sets validThrough.
138
     *
139
     * @param string $validThrough
140
     * @param $job \JobApis\Jobs\Client\Job
141
     *
142
     * @return $job
0 ignored issues
show
Documentation introduced by
The doc-type $job could not be parsed: Unknown type name "$job" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
143
     */
144 4
    protected function setValidThroughAsString($validThrough, $job)
145
    {
146 4
        if (strtotime($validThrough) !== false) {
147 4
            $job->validThrough = new \DateTime($validThrough);
148 4
        }
149
150 4
        return $job;
151
    }
152
}
153