Passed
Branch feature/first-release (57b0a8)
by Andrea Marco
13:40
created

SourceWrapper::json()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Cerbero\LazyJsonPages;
4
5
use Cerbero\LazyJsonPages\Exceptions\LazyJsonPagesException;
6
use GuzzleHttp\Client;
7
use Illuminate\Http\Client\Response;
8
use Illuminate\Support\Arr;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * The source wrapper.
14
 *
15
 */
16
class SourceWrapper
17
{
18
    /**
19
     * The original source.
20
     *
21
     * @var RequestInterface|Response
22
     */
23
    public $original;
24
25
    /**
26
     * The HTTP request.
27
     *
28
     * @var RequestInterface
29
     */
30
    public $request;
31
32
    /**
33
     * The HTTP response.
34
     *
35
     * @var ResponseInterface
36
     */
37
    public $response;
38
39
    /**
40
     * The decoded JSON.
41
     *
42
     * @var array
43
     */
44
    protected $decodedJson;
45
46
    /**
47
     * Instantiate the class.
48
     *
49
     * @param RequestInterface|Response $source
50
     *
51
     * @throws LazyJsonPagesException
52
     */
53
    public function __construct($source)
54
    {
55
        if (!$source instanceof RequestInterface && !$source instanceof Response) {
0 ignored issues
show
introduced by
$source is always a sub-type of Illuminate\Http\Client\Response.
Loading history...
56
            throw new LazyJsonPagesException('The provided JSON source is not valid.');
57
        }
58
59
        $this->original = $source;
60
        $this->request = $this->getSourceRequest();
61
        $this->response = $this->getSourceResponse();
62
    }
63
64
    /**
65
     * Retrieve the HTTP request of the source
66
     *
67
     * @return RequestInterface
68
     *
69
     * @throws LazyJsonPagesException
70
     */
71
    protected function getSourceRequest(): RequestInterface
72
    {
73
        if ($this->original instanceof RequestInterface) {
74
            return $this->original;
75
        } elseif (isset($this->original->transferStats)) {
76
            return $this->original->transferStats->getRequest();
77
        }
78
79
        throw new LazyJsonPagesException('The HTTP client response is not aware of the original request.');
80
    }
81
82
    /**
83
     * Retrieve the HTTP response of the source
84
     *
85
     * @return ResponseInterface
86
     */
87
    protected function getSourceResponse(): ResponseInterface
88
    {
89
        if ($this->original instanceof RequestInterface) {
90
            return (new Client())->send($this->original);
91
        }
92
93
        return $this->original->toPsrResponse();
94
    }
95
96
    /**
97
     * Retrieve a fragment of the decoded JSON
98
     *
99
     * @param string $path
100
     * @param mixed $default
101
     * @return mixed
102
     */
103
    public function json(string $path, $default = null)
104
    {
105
        if (!isset($this->decodedJson)) {
106
            $this->decodedJson = json_decode((string) $this->response->getBody(), true);
107
        }
108
109
        return Arr::get($this->decodedJson, $path, $default);
110
    }
111
}
112