GovtProvider::createJobObject()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 22
cts 22
cp 1
rs 9.472
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3
1
<?php namespace JobApis\Jobs\Client\Providers;
2
3
use JobApis\Jobs\Client\Job;
4
use JobApis\Jobs\Client\Collection;
5
6
class GovtProvider extends AbstractProvider
7
{
8
    /**
9
     * Takes a job valid for multiple locations and turns it into multiple jobs
10
     *
11
     * @param array $item
12
     *
13
     * @return array
14
     */
15 6
    public function createJobArray($item)
16
    {
17 6
        $jobs = [];
18 6
        if (isset($item['locations']) && count($item['locations']) > 1) {
19 2
            foreach ($item['locations'] as $location) {
20 2
                $item['location'] = $location;
21 2
                $jobs[] = $item;
22 2
            }
23 2
        } else {
24 4
            $item['location'] = $item['locations'][0];
25 4
            $jobs[] = $item;
26
        }
27 6
        return $jobs;
28
    }
29
30
    /**
31
     * Returns the standardized job object
32
     *
33
     * @param array $payload
34
     *
35
     * @return \JobApis\Jobs\Client\Job
36
     */
37 4
    public function createJobObject($payload)
38
    {
39 4
        $job = new Job([
40 4
            'sourceId' => $payload['id'],
41 4
            'title' => $payload['position_title'],
42 4
            'name' => $payload['position_title'],
43 4
            'url' => $payload['url'],
44 4
            'location' => $payload['location'],
45 4
            'maximumSalary' => $payload['maximum'],
46 4
            'startDate' => $payload['start_date'],
47 4
            'endDate' => $payload['end_date'],
48 4
        ]);
49
50 4
        $location = static::parseLocation($payload['location']);
51
52 4
        $job->setCompany($payload['organization_name'])
53 4
            ->setDatePostedAsString($payload['start_date'])
54 4
            ->setMinimumSalary($payload['minimum']);
55
56 4
        if (isset($location[0])) {
57 4
            $job->setCity($location[0]);
58 4
        }
59 4
        if (isset($location[1])) {
60 4
            $job->setState($location[1]);
61 4
        }
62
63 4
        return $job;
64
    }
65
66
    /**
67
     * Job response object default keys that should be set
68
     *
69
     * @return  array
70
     */
71 4
    public function getDefaultResponseFields()
72
    {
73
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('id', 'posi... 'minimum', 'maximum'); (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...
74 4
            'id',
75 4
            'position_title',
76 4
            'organization_name',
77 4
            'location',
78 4
            'start_date',
79 4
            'end_date',
80 4
            'url',
81 4
            'minimum',
82
            'maximum'
83 4
        ];
84
    }
85
86
    /**
87
     * Get listings path
88
     *
89
     * @return  string
90
     */
91 4
    public function getListingsPath()
92
    {
93 4
        return '';
94
    }
95
96
    /**
97
     * Create and get collection of jobs from given listings
98
     *
99
     * @param  array $listings
100
     *
101
     * @return Collection
102
     */
103 2
    protected function getJobsCollectionFromListings(array $listings = array())
104
    {
105 2
        $collection = new Collection;
106 2
        array_map(function ($item) use ($collection) {
107 2
            $jobs = $this->createJobArray($item);
108 2
            foreach ($jobs as $item) {
109 2
                $item = static::parseAttributeDefaults($item, $this->getDefaultResponseFields());
110 2
                $job = $this->createJobObject($item);
111 2
                $job->setQuery($this->query->getKeyword())
112 2
                    ->setSource($this->getSource());
113 2
                $collection->add($job);
114 2
            }
115 2
        }, $listings);
116 2
        return $collection;
117
    }
118
}
119