CareersInGovProvider   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 85
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createJobObject() 0 14 1
A getDefaultResponseFields() 0 9 1
A getFormat() 0 4 1
A getJobs() 0 18 3
A getListingsPath() 0 4 1
1
<?php namespace JobApis\Jobs\Client\Providers;
2
3
use JobApis\Jobs\Client\Exceptions\MissingParameterException;
4
use JobApis\Jobs\Client\Job;
5
6
class CareersInGovProvider extends AbstractProvider
7
{
8
    /**
9
     * Returns the standardized job object
10
     *
11
     * @param array $payload
12
     *
13
     * @return \JobApis\Jobs\Client\Job
14
     */
15
    public function createJobObject($payload)
16
    {
17
        $job = new Job([
18
            'description' => trim($payload['description']),
19
            'name' => $payload['title'],
20
            'title' => $payload['title'],
21
            'url' => $payload['link'],
22
        ]);
23
24
        // Set date posted
25
        $job->setDatePostedAsString($payload['pubDate']);
26
27
        return $job;
28
    }
29
30
    /**
31
     * Job response object default keys that should be set
32
     *
33
     * @return  array
34
     */
35
    public function getDefaultResponseFields()
36
    {
37
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('descriptio...', 'pubDate', 'title'); (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...
38
            'description',
39
            'link',
40
            'pubDate',
41
            'title',
42
        ];
43
    }
44
45
    /**
46
     * Get format
47
     *
48
     * @return  string Currently only 'json' and 'xml' supported
49
     */
50
    public function getFormat()
51
    {
52
        return 'xml';
53
    }
54
55
    /**
56
     * Makes the api call and returns a collection of job objects
57
     * NOTE: This method overwrites the AbstractProvider in order to trim whitespace from the body
58
     *
59
     * @return  \JobApis\Jobs\Client\Collection
60
     * @throws MissingParameterException
61
     */
62
    public function getJobs()
63
    {
64
        // Verify that all required query vars are set
65
        if ($this->query->isValid()) {
66
            // Get the response from the client using the query
67
            $response = $this->getClientResponse();
68
            // Get the response body as a string
69
            $body = trim((string) $response->getBody());
70
            // Parse the string
71
            $payload = $this->parseAsFormat($body, $this->getFormat());
72
            // Gets listings if they're nested
73
            $listings = is_array($payload) ? $this->getRawListings($payload) : [];
74
            // Return a job collection
75
            return $this->getJobsCollectionFromListings($listings);
76
        } else {
77
            throw new MissingParameterException("All Required parameters for this provider must be set");
78
        }
79
    }
80
81
    /**
82
     * Get listings path
83
     *
84
     * @return  string
85
     */
86
    public function getListingsPath()
87
    {
88
        return 'channel.item';
89
    }
90
}
91