Completed
Push — master ( 0d4b0a...562840 )
by Justin
03:15
created

PageApi::call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3.0416
1
<?php
2
3
namespace Vessel\Render;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Exception\ConnectException;
8
use Journey\Cache\CacheAdapterInterface;
9
10
class PageApi
11
{
12
    /**
13
     * Guzzle HTTP interface.
14
     *
15
     * @var \GuzzleHTTP\Client
16
     */
17
    protected $http;
18
19
    /**
20
     * A cache adapter for storing results.
21
     *
22
     * @var \Journey\Cache\CacheAdapterInterface
23
     */
24
    protected $cache;
25
26
    /**
27
     * Initialize the page api methods.
28
     */
29 3
    public function __construct(array $config)
30
    {
31 3
        $this->cache = $config['cache'];
32 3
        $this->http = new Client([
33 3
            'base_uri' => $config['base_uri']
34 3
        ]);
35 3
    }
36
37
    /**
38
     * Get a particular page from the api.
39
     *
40
     * @param  string $path path of the page
41
     * @return array|false
42
     */
43 3
    public function getPage($path)
44
    {
45 3
        return $this->call("get", "/api/pages?slug=" . $path);
46
    }
47
48
    /**
49
     * Call a particular api endpoint.
50
     *
51
     * @return \Psr\Http\Message\ResponseInterface
52
     */
53 3
    public function call($method, $url)
54
    {
55
        try {
56 3
            return $this->http->request(strtoupper($method), $url);
57 1
        } catch (ClientException $e) {
58
            return $e->getResponse();
59 1
        } catch (ConnectException $e) {
60 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Vessel\Render\PageApi::call of type Psr\Http\Message\ResponseInterface|null.

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...
61
        }
62
    }
63
}
64