J2cProvider::createJobArray()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.5021

Importance

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