Completed
Push — master ( 519a7f...4aeb8b )
by Justin
02:11
created

PageApi::getPagesByType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Vssl\Render;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Exception\ConnectException;
8
use Journey\Cache\CacheAdapterInterface;
9
use Psr\Http\Message\RequestInterface;
10
11
class PageApi
12
{
13
    /**
14
     * Guzzle HTTP interface.
15
     *
16
     * @var \GuzzleHTTP\Client
17
     */
18
    protected $http;
19
20
    /**
21
     * A cache adapter for storing results.
22
     *
23
     * @var \Journey\Cache\CacheAdapterInterface
24
     */
25
    protected $cache;
26
27
    /**
28
     * Initialize the page api methods.
29
     */
30 6
    public function __construct(RequestInterface $request, array $config)
31
    {
32 6
        $this->cache = $config['cache'];
33 6
        $this->http = new Client([
34 6
            'base_uri' => $config['base_uri'],
35
            'headers' => [
36 6
                'X-Render-Host' => $request->getUri()->getHost(),
37
            ]
38 6
        ]);
39 6
    }
40
41
    /**
42
     * Get a particular page from the api.
43
     *
44
     * @param  string $path path of the page
45
     * @return array|false
46
     */
47 4
    public function getPage($path)
48
    {
49 4
        return $this->call("get", "/api/pages?slug=" . $path);
50
    }
51
52
    /**
53
     * Get a particular page from the api.
54
     *
55
     * @param  array $ids unique ids of pages to fetch data for.
56
     * @return array|false
57
     */
58
    public function getPagesById($ids)
59
    {
60 2
        $ids = array_filter(array_map(function ($id) {
61 2
            return is_numeric($id) ? (integer) $id : null;
62 2
        }, $ids));
63 2
        return $this->call("get", "/api/pages?ids=" . implode(",", $ids));
64
    }
65
66
    /**
67
     * Get a list of pages of a particular type from the api.
68
     *
69
     * @param  string $type type of pages to get.
70
     * @return array
71
     */
72
    public function getPagesByType($type)
73
    {
74
        return $this->call("get", "/api/pages?type=" . urlencode($type));
75
    }
76
77
    /**
78
     * Call a particular api endpoint.
79
     *
80
     * @return \Psr\Http\Message\ResponseInterface
81
     */
82 6
    public function call($method, $url)
83
    {
84
        try {
85 6
            return $this->http->request(strtoupper($method), $url);
86 1
        } catch (ClientException $e) {
87
            return $e->getResponse();
88 1
        } catch (ConnectException $e) {
89 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 Vssl\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...
90
        }
91
    }
92
}
93