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

Resolver::config()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Vessel\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 \Vessel\Render\PageApi
31
     */
32
    protected $api;
33
34
    /**
35
     * Initialize a new Resolver
36
     */
37 3
    public function __construct(ServerRequestInterface $request, $config = false)
38
    {
39 3
        $this->request = $request;
40 3
        $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 3
        if (!$this->config['cache'] instanceof CacheAdapterInterface) {
42
            throw new ResolverException('Cache must implement \Journey\Cache\CacheAdapterInterface.');
43
        }
44 3
        $this->api = new PageApi($this->config);
45 3
    }
46
47
    /**
48
     * Get a modified request message.
49
     *
50
     * @return \Psr\Http\Message\ServerRequestInterface
51
     */
52 3
    public function getRequest()
53
    {
54 3
        return $this->request->withAttribute('ws-page', $this->resolve());
55
    }
56
57
    /**
58
     * Resolve the current page from WebStories.
59
     *
60
     * @return array
61
     */
62 3
    public function resolve()
63
    {
64 3
        if ($response = $this->api->getPage($this->request->getUri()->getPath())) {
65 2
            $data = $this->decodeBody($response);
66 1
            return [
67 2
                'status' => $response->getStatusCode(),
68 2
                'data' => $data,
69 2
                'page' => is_array($data) ? new Renderer($this->config, $data) : $data,
70 2
            ];
71
        }
72 1
        return false;
73
    }
74
75
    /**
76
     * Decode the body of a particular request.
77
     *
78
     * @return mixed
79
     */
80 2
    public function decodeBody(ResponseInterface $response)
81
    {
82 2
        $body = (string) $response->getBody();
83 2
        if ($response->getHeaderLine('Content-Type') == "application/json") {
84 2
            return json_decode($body, true);
85
        }
86
        return $body;
87
    }
88
89
    /**
90
     * Configure the resolver.
91
     *
92
     * @return void
93
     */
94 9
    public static function config($assign = false)
95
    {
96 9
        static $config;
97
98 9
        if (is_array($assign) || !$config) {
99 4
            $config = array_merge([
100 4
                'cache' => null,
101 4
                'base_uri' => 'https://www.webstories.com/',
102
                'required_fields' => [
103 4
                    'id',
104 4
                    'title',
105
                    'stripes'
106 4
                ],
107
                // '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...
108 4
            ], $assign);
109 4
        }
110 9
        return $config ?: [];
111
    }
112
}
113