Resolver::resolve()   B
last analyzed

Complexity

Conditions 8
Paths 33

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 7.7777
c 1
b 0
f 0
cc 8
eloc 15
nc 33
nop 0
crap 8
1
<?php
2
3
namespace Vssl\Render;
4
5
use Journey\Cache\CacheAdapterInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
9
class Resolver
10
{
11
    /**
12
     * The request object.
13
     *
14
     * @var \Psr\Http\Message\ServerRequestInterface
15
     */
16
    protected $request;
17
18
    /**
19
     * Configuration array.
20
     *
21
     * @var array
22
     */
23
    protected $config;
24
25
    /**
26
     * The page api object.
27
     *
28
     * @var \Vssl\Render\PageApi
29
     */
30
    protected $api;
31
32
    /**
33
     * Initialize a new Resolver
34
     */
35 11
    public function __construct(ServerRequestInterface $request, $config = false)
36
    {
37 11
        $this->request = $request;
38 11
        $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...
39 11
        if (!$this->config['cache'] instanceof CacheAdapterInterface) {
40 1
            throw new ResolverException('Cache must implement \Journey\Cache\CacheAdapterInterface.');
41 1
        }
42 11
        $this->api = new PageApi($request, $this->config);
43 11
    }
44
45
    /**
46
     * Get a modified request message.
47
     *
48
     * @return \Psr\Http\Message\ServerRequestInterface
49
     */
50 4
    public function getRequest()
51
    {
52 4
        return $this->request->withAttribute('vssl-page', $this->resolve());
53
    }
54
55
    /**
56
     * Get an instance of the PageApi class.
57
     *
58
     * @return \Vssl\Render\PageApi
59
     */
60 3
    public function getPageApi()
61
    {
62 3
        return $this->api;
63
    }
64
65
    /**
66
     * Get the configuration settings.
67
     *
68
     * @return array
69
     */
70 1
    public function getConfig()
71
    {
72 1
        return $this->config;
73
    }
74
75
    /**
76
     * Get the configured CacheAdapterInterface.
77
     *
78
     * @return \Journey\Cache\CacheAdapterInterface
79
     */
80 1
    public function getCache()
81
    {
82 1
        return $this->config['cache'];
83
    }
84
85
    /**
86
     * Resolve the current page from WebStories. Returns a render able page or
87
     * false.
88
     *
89
     * @return array
90
     */
91 4
    public function resolve()
92
    {
93 4
        $response = $this->api->getPage($this->request->getUri()->getPath());
94 4
        if ($response && ($page = $this->decodePage($response))) {
95 3
            $status = $response->getStatusCode();
96
            return [
97 3
                'status' => $status,
98 3
                'data' => $page,
99 3
                'page' => (is_array($page) && $status == 200) ? new Renderer($this->config, $page) : $page,
100 3
                'metadata' => (is_array($page) && $status == 200) ? new Metadata($this->config, $page) : $page,
101 3
                'type' => !empty($page['type']) ? $page['type'] : false
102 3
            ];
103
        }
104
        return [
105 1
            'status' => false,
106 1
            'error' => 'An unknown error occurred',
107 1
            'data' => [],
108
            'type' => false
109 1
        ];
110
    }
111
112
    /**
113
     * Decode the body of a particular request.
114
     *
115
     * @return mixed
116
     */
117 4
    public function decodePage(ResponseInterface $response)
118
    {
119 4
        $body = (string) $response->getBody();
120 4
        if ($response->getHeaderLine('Content-Type') == "application/json") {
121 3
            $page = json_decode($body, true);
122 3
            return !empty($page['exists']) ? $page['page'] : false;
123
        }
124 1
        return false;
125
    }
126
127
    /**
128
     * Configure the resolver.
129
     *
130
     * @return void
131
     */
132 24
    public static function config($assign = false)
133
    {
134 24
        static $config;
135
136 24
        if (is_array($assign) || !$config) {
137 17
            $config = array_merge([
138 17
                'cache' => null,
139 17
                'cache_ttl' => false,
140 17
                'base_uri' => 'https://pages.vssl.io/',
141
                'required_fields' => [
142 17
                    'id',
143 17
                    'title',
144
                    'stripes'
145 17
                ],
146
                // '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...
147 17
            ], $assign);
148 17
        }
149 24
        return $config ?: [];
150
    }
151
}
152