MuseProvider   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 140
ccs 58
cts 58
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B createJobObject() 0 24 1
A getDefaultResponseFields() 0 18 1
A getListingsPath() 0 4 1
A setCategories() 0 11 3
A setCompany() 0 7 3
A setLevels() 0 11 3
A setLocation() 0 7 3
1
<?php namespace JobApis\Jobs\Client\Providers;
2
3
use JobApis\Jobs\Client\Job;
4
5
class MuseProvider extends AbstractProvider
6
{
7
    /**
8
     * Returns the standardized job object
9
     *
10
     * @param array $payload Raw job payload from the API
11
     *
12
     * @return \JobApis\Jobs\Client\Job
13
     */
14 4
    public function createJobObject($payload = [])
15
    {
16 4
        $job = new Job([
17 4
            'title' => $payload['name'],
18 4
            'name' => $payload['name'],
19 4
            'description' => $payload['contents'],
20 4
            'url' => $payload['refs']['landing_page'],
21 4
            'sourceId' => $payload['id'],
22 4
        ]);
23
24
        // categories array
25 4
        $this->setCategories($job, $payload['categories']);
26
27
        // company array
28 4
        $this->setCompany($job, $payload['company']);
29
30
        // levels array
31 4
        $this->setLevels($job, $payload['levels']);
32
33
        // locations array
34 4
        $this->setLocation($job, $payload['locations']);
35
36 4
        return $job;
37
    }
38
39
    /**
40
     * Job response object default keys that should be set
41
     *
42
     * @return  string
43
     */
44 4
    public function getDefaultResponseFields()
45
    {
46
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('levels', '...ompany', 'id', 'name'); (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...
47 4
            'levels', // array
48 4
            'locations', // array
49 4
            'tags', // array
50 4
            'categories', // array
51 4
            'publication_date',
52 4
            'short_name',
53 4
            'refs', // array
54 4
            'contents',
55 4
            'type',
56 4
            'model_type',
57 4
            'company', // array
58 4
            'id',
59 4
            'name',
60 4
        ];
61
    }
62
63
    /**
64
     * Get listings path
65
     *
66
     * @return  string
67
     */
68 4
    public function getListingsPath()
69
    {
70 4
        return 'results';
71
    }
72
73
    /**
74
     * Sets the categories on the job using the categories array
75
     *
76
     * @param Job $job
77
     * @param array $categories
78
     *
79
     * @return MuseProvider
80
     */
81 4
    protected function setCategories(Job $job, $categories = [])
82
    {
83 4
        $occupationalCats = [];
84 4
        foreach ($categories as $category) {
85 4
            $occupationalCats[] = $category['name'];
86 4
        }
87 4
        if ($occupationalCats) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $occupationalCats of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
88 4
            $job->setOccupationalCategory(implode(', ', $occupationalCats));
89 4
        }
90 4
        return $this;
91
    }
92
93
    /**
94
     * Sets the company on the job using the company array
95
     *
96
     * @param Job $job
97
     * @param array $company
98
     *
99
     * @return MuseProvider
100
     */
101 4
    protected function setCompany(Job $job, $company = [])
102
    {
103 4
        if ($company && isset($company['name'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $company of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
104 4
            $job->setCompany($company['name']);
105 4
        }
106 4
        return $this;
107
    }
108
109
    /**
110
     * Sets the experience levels on the job using the levels array
111
     *
112
     * @param Job $job
113
     * @param array $levels
114
     *
115
     * @return MuseProvider
116
     */
117 4
    protected function setLevels(Job $job, $levels = [])
118
    {
119 4
        $requirements = [];
120 4
        foreach ($levels as $level) {
121 4
            $requirements[] = $level['name'];
122 4
        }
123 4
        if ($requirements) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $requirements of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
124 4
            $job->setExperienceRequirements(implode(', ', $requirements));
125 4
        }
126 4
        return $this;
127
    }
128
129
    /**
130
     * Sets the location on the job using the first location in the array
131
     *
132
     * @param Job $job
133
     * @param array $locations
134
     *
135
     * @return MuseProvider
136
     */
137 4
    protected function setLocation(Job $job, $locations = [])
138
    {
139 4
        if (isset($locations[0]) && isset($locations[0]['name'])) {
140 4
            $job->setLocation($locations[0]['name']);
141 4
        }
142 4
        return $this;
143
    }
144
}
145