Passed
Push — master ( 38f6bd...af01db )
by Justin
06:43
created

Resolver::getPageApi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Vssl\Render;
4
5
use GuzzleHttp\Client;
6
use Journey\Cache\Adapters\LocalAdapter;
7
use Journey\Cache\CacheAdapterInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
11
class Resolver
12
{
13
    /**
14
     * The request object.
15
     *
16
     * @var \Psr\Http\Message\ServerRequestInterface
17
     */
18
    protected $request;
19
20
    /**
21
     * Configuration array.
22
     *
23
     * @var array
24
     */
25
    protected $config;
26
27
    /**
28
     * The page api object.
29
     *
30
     * @var \Vssl\Render\PageApi
31
     */
32
    protected $api;
33
34
    /**
35
     * Initialize a new Resolver
36
     */
37 6
    public function __construct(ServerRequestInterface $request, $config = false)
38
    {
39 6
        $this->request = $request;
40 6
        $this->config = is_array($config) ? static::config($config) : static::config();
0 ignored issues
show
Documentation introduced by
$config is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41 6
        if (!$this->config['cache'] instanceof CacheAdapterInterface) {
42
            throw new ResolverException('Cache must implement \Journey\Cache\CacheAdapterInterface.');
43
        }
44 6
        $this->api = new PageApi($request, $this->config);
45 6
    }
46
47
    /**
48
     * Get a modified request message.
49
     *
50
     * @return \Psr\Http\Message\ServerRequestInterface
51
     */
52 4
    public function getRequest()
53
    {
54 4
        return $this->request->withAttribute('vssl-page', $this->resolve());
55
    }
56
57
    /**
58
     * Get an instance of the PageApi class.
59
     *
60
     * @return \Vssl\Render\PageApi
61
     */
62 2
    public function getPageApi()
63
    {
64 2
        return $this->api;
65
    }
66
67
    /**
68
     * Resolve the current page from WebStories. Returns a render able page or
69
     * false.
70
     *
71
     * @return array
72
     */
73 4
    public function resolve()
74
    {
75 4
        $response = $this->api->getPage($this->request->getUri()->getPath());
76 4
        if ($response && ($page = $this->decodePage($response))) {
77 3
            $status = $response->getStatusCode();
78
            return [
79 3
                'status' => $status,
80 3
                'data' => $page,
81 3
                'page' => (is_array($page) && $status == 200) ? new Renderer($this->config, $page) : $page,
82 3
                'metadata' => (is_array($page) && $status == 200) ? new Metadata($this->config, $page) : $page,
83 3
                'type' => !empty($page['type']) ? $page['type'] : false
84 3
            ];
85
        }
86
        return [
87 1
            'status' => false,
88 1
            'error' => 'An unknown error occurred',
89 1
            'data' => []
90 1
        ];
91
    }
92
93
    /**
94
     * Decode the body of a particular request.
95
     *
96
     * @return mixed
97
     */
98 3
    public function decodePage(ResponseInterface $response)
99
    {
100 3
        $body = (string) $response->getBody();
101 3
        if ($response->getHeaderLine('Content-Type') == "application/json") {
102 3
            $page = json_decode($body, true);
103 3
            return !empty($page['exists']) ? $page['page'] : false;
104
        }
105
        return false;
106
    }
107
108
    /**
109
     * Configure the resolver.
110
     *
111
     * @return void
112
     */
113 18
    public static function config($assign = false)
114
    {
115 18
        static $config;
116
117 18
        if (is_array($assign) || !$config) {
118 12
            $config = array_merge([
119 12
                'cache' => null,
120 12
                'base_uri' => 'https://pages.vssl.io/',
121
                'required_fields' => [
122 12
                    'id',
123 12
                    'title',
124
                    'stripes'
125 12
                ],
126
                // 'templates' => 'your-directory'
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
127 12
            ], $assign);
128 12
        }
129 18
        return $config ?: [];
130
    }
131
}
132