Completed
Push — master ( 6cbf0d...65d54a )
by Karl
9s
created

IndeedProvider::createJobObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 16
cts 16
cp 1
rs 9.3142
cc 1
eloc 15
nc 1
nop 1
crap 1
1
<?php namespace JobApis\Jobs\Client\Providers;
2
3
use JobApis\Jobs\Client\Job;
4
5
class IndeedProvider 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
            'title' => $payload['jobtitle'],
18 4
            'name' => $payload['jobtitle'],
19 4
            'description' => $payload['snippet'],
20 4
            'url' => $payload['url'],
21 4
            'sourceId' => $payload['jobkey'],
22 4
            'location' => $payload['formattedLocation'],
23 4
        ]);
24
25 4
        $job = $this->setJobLocation($job, $payload['formattedLocation']);
26
27 4
        $postalCode = str_replace($payload['formattedLocation'].' ', '', $payload['formattedLocationFull']);
28
29 4
        return $job->setCompany($payload['company'])
30 4
            ->setDatePostedAsString($payload['date'])
31 4
            ->setPostalCode($postalCode)
32 4
            ->setLatitude($payload['latitude'])
33 4
            ->setLongitude($payload['longitude']);
34
    }
35
36
    /**
37
     * Job response object default keys that should be set
38
     *
39
     * @return  string
40
     */
41 4
    public function getDefaultResponseFields()
42
    {
43
        return [
0 ignored issues
show
Bug Best Practice introduced by Karl
The return type of return array('jobtitle',...atitude', 'longitude'); (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...
44 4
            'jobtitle',
45 4
            'company',
46 4
            'formattedLocation',
47 4
            'formattedLocationFull',
48 4
            'source',
49 4
            'date',
50 4
            'snippet',
51 4
            'url',
52 4
            'jobkey',
53 4
            'latitude',
54
            'longitude'
55 4
        ];
56
    }
57
58
    /**
59
     * Get listings path
60
     *
61
     * @return  string
62
     */
63 4
    public function getListingsPath()
64
    {
65 4
        return 'results';
66
    }
67
68
    /**
69
     * Attempt to parse and add location to Job
70
     *
71
     * @param Job     $job
72
     * @param string  $location
73
     *
74
     * @return  Job
75
     */
76 4
    private function setJobLocation(Job $job, $location)
77
    {
78 4
        $location = static::parseLocation($location);
79
80 4
        if (isset($location[0])) {
81 4
            $job->setCity($location[0]);
82 4
        }
83 4
        if (isset($location[1])) {
84 4
            $job->setState($location[1]);
85 4
        }
86
87 4
        return $job;
88
    }
89
}
90