UsajobsProvider   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 2
dl 0
loc 178
ccs 86
cts 86
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createJobObject() 0 20 1
A getDefaultResponseFields() 0 7 1
A getListingsPath() 0 4 1
A setDates() 0 16 3
B setNestedProperties() 0 32 5
B setSalary() 0 11 5
C setLocation() 0 24 8
1
<?php namespace JobApis\Jobs\Client\Providers;
2
3
use JobApis\Jobs\Client\Job;
4
5
class UsajobsProvider extends AbstractProvider
6
{
7
    /**
8
     * Returns the standardized job object
9
     *
10
     * NOTE: The following properties are not being set:
11
     * PositionID
12
     * DepartmentName
13
     * ApplyURI
14
     * UserArea
15
     *
16
     * @param array $payload Raw job payload from the API
17
     *
18
     * @return \JobApis\Jobs\Client\Job
19
     */
20 4
    public function createJobObject($payload = [])
21
    {
22 4
        $id = $payload['MatchedObjectId'];
23 4
        $payload = $payload['MatchedObjectDescriptor'];
24
25 4
        $job = new Job([
26 4
            'sourceId' => $id,
27 4
            'title' => $payload['PositionTitle'],
28 4
            'name' => $payload['PositionTitle'],
29 4
            'url' => $payload['PositionURI'],
30 4
            'qualifications' => $payload['QualificationSummary'],
31 4
        ]);
32
33 4
        $job->setCompany($payload['OrganizationName']);
34
35 4
        $job = $this->setDates($payload, $job);
36 4
        $job = $this->setNestedProperties($payload, $job);
37 4
        $job = $this->setSalary($payload['PositionRemuneration'], $job);
38 4
        return $this->setLocation($payload['PositionLocation'], $job);
39
    }
40
41
    /**
42
     * Job response object default keys that should be set
43
     *
44
     * @return  array
45
     */
46 4
    public function getDefaultResponseFields()
47
    {
48
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('MatchedObj...chedObjectDescriptor'); (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...
49 4
            'MatchedObjectId',
50 4
            'MatchedObjectDescriptor',
51 4
        ];
52
    }
53
54
    /**
55
     * Get listings path
56
     *
57
     * @return string
58
     */
59 4
    public function getListingsPath()
60
    {
61 4
        return 'SearchResult.SearchResultItems';
62
    }
63
64
    /**
65
     * Sets nested properties
66
     *
67
     * @param $payload array
68
     * @param $job \JobApis\Jobs\Client\Job
69
     *
70
     * @return \JobApis\Jobs\Client\Job
71
     */
72 4
    protected function setDates($payload, $job)
73
    {
74
        $dateFields = [
75 4
            'PublicationStartDate' => 'datePosted',
76 4
            'ApplicationCloseDate' => 'validThrough',
77 4
            'PositionStartDate' => 'startDate',
78 4
            'PositionEndDate' => 'endDate',
79 4
        ];
80 4
        foreach ($dateFields as $key => $field) {
81 4
            if (strtotime($payload[$key]) !== false) {
82 4
                $job->{'set'.ucfirst($field)}(new \DateTime($payload[$key]));
83 4
            }
84 4
        }
85
86 4
        return $job;
87
    }
88
89
    /**
90
     * Sets nested properties
91
     *
92
     * @param $payload array
93
     * @param $job \JobApis\Jobs\Client\Job
94
     *
95
     * @return \JobApis\Jobs\Client\Job
96
     */
97 4
    protected function setNestedProperties($payload, $job)
98
    {
99
        $nestedProperties = [
100
            'PositionFormattedDescription' => [
101 4
                'attribute' => 'Content',
102 4
                'property' => 'description',
103 4
            ],
104
            'PositionSchedule' => [
105 4
                'attribute' => 'Name',
106 4
                'property' => 'employmentType',
107 4
            ],
108
            'JobCategory' => [
109 4
                'attribute' => 'Name',
110 4
                'property' => 'occupationalCategory',
111 4
            ],
112
            'PositionOfferingType' => [
113 4
                'attribute' => 'Name',
114 4
                'property' => 'additionalType',
115 4
            ],
116 4
        ];
117
118 4
        foreach ($nestedProperties as $property => $value) {
119 4
            if (isset($payload[$property])
120 4
                && isset($payload[$property][0])
121 4
                && isset($payload[$property][0][$value['attribute']])
122 4
            ) {
123 4
                $job->{"set".ucfirst($value['property'])}($payload[$property][0][$value['attribute']]);
124 4
            }
125 4
        }
126
127 4
        return $job;
128
    }
129
130
    /**
131
     * Parses the salary range and adds it to the job
132
     *
133
     * @param $salaries array
134
     * @param $job \JobApis\Jobs\Client\Job
135
     *
136
     * @return \JobApis\Jobs\Client\Job
137
     */
138 4
    protected function setSalary($salaries, $job)
139
    {
140 4
        if (isset($salaries[0]) && isset($salaries[0]['MinimumRange'])) {
141 4
            $job->setMinimumSalary($salaries[0]['MinimumRange']);
142 4
        }
143 4
        if (isset($salaries[0]) && isset($salaries[0]['MaximumRange'])) {
144 4
            $job->setMaximumSalary($salaries[0]['MaximumRange']);
145 4
        }
146
        // NOTE: Can also get pay shedule from $salaries[0]["RateIntervalCode"]
147 4
        return $job;
148
    }
149
150
    /**
151
     * Parses the location and attaches it to the job
152
     *
153
     * @param $locations array
154
     * @param $job \JobApis\Jobs\Client\Job
155
     *
156
     * @return \JobApis\Jobs\Client\Job
157
     */
158 4
    protected function setLocation($locations, $job)
159
    {
160 4
        if (isset($locations[0])) {
161 4
            if (isset($locations[0]['LocationName'])) {
162 4
                $job->setLocation($locations[0]['LocationName']);
163 4
            }
164 4
            if (isset($locations[0]['CountryCode'])) {
165 4
                $job->setCountry($locations[0]['CountryCode']);
166 4
            }
167 4
            if (isset($locations[0]['CountrySubDivisionCode'])) {
168 4
                $job->setState($locations[0]['CountrySubDivisionCode']);
169 4
            }
170 4
            if (isset($locations[0]['CityName'])) {
171 4
                $job->setCity($locations[0]['CityName']);
172 4
            }
173 4
            if (isset($locations[0]['Longitude'])) {
174 4
                $job->setLongitude($locations[0]['Longitude']);
175 4
            }
176 4
            if (isset($locations[0]['Latitude'])) {
177 4
                $job->setLatitude($locations[0]['Latitude']);
178 4
            }
179 4
        }
180 4
        return $job;
181
    }
182
}
183