Passed
Push — master ( b5344a...aeae0c )
by Dani
01:38
created

src/Http/Response.php (2 issues)

1
<?php
2
3
namespace Postpay\Http;
4
5
use Postpay\Exceptions\GraphQLException;
6
use Postpay\Exceptions\RESTfulException;
7
8
class Response
9
{
10
    /**
11
     * @var \Postpa\Http\Request The original request.
0 ignored issues
show
The type Postpa\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
     */
13
    protected $request;
14
15
    /**
16
     * @var int The HTTP response status code.
17
     */    
18
    protected $statusCode;
19
20
    /**
21
     * @var array The headers returned from API.
22
     */
23
    protected $headers;
24
25
    /**
26
     * @var string The raw body of the response.
27
     */
28
    protected $body;
29
30
    /**
31
     * @var array The decoded body.
32
     */
33
    protected $decodedBody = [];
34
35
    /**
36
     * @var string The exception type to be thrown.
37
     */
38
    protected $exc;
39
40
    /**
41
     * @var \Postpay\Exceptions\PostpayException The exception thrown.
42
     */
43
    protected $thrownException;
44
45
    /**
46
     * Creates a new Response entity.
47
     *
48
     * @param \Postpay\Http\Request $request
49
     * @param int|null              $statusCode
50
     * @param array|null            $headers
51
     * @param string|null           $body
52
     */
53
    public function __construct(
54
        Request $request,
55
        $statusCode = null,
56
        array $headers = [],
57
        $body = null
58
    ) {
59
        $this->request = $request;
0 ignored issues
show
Documentation Bug introduced by
It seems like $request of type Postpay\Http\Request is incompatible with the declared type Postpa\Http\Request of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60
        $this->statusCode = $statusCode;
61
        $this->headers = $headers;
62
        $this->body = $body;
63
64
        if ($this->request->isGraphQL()) {
65
            $this->exc = GraphQLException::class;
66
        } else {
67
            $this->exc = RESTfulException::class;
68
        }
69
        $this->decodeBody();
70
    }
71
72
    /**
73
     * Return the original request that returned this response.
74
     *
75
     * @return \Postpay\Http\Request
76
     */
77
    public function getRequest()
78
    {
79
        return $this->request;
80
    }
81
82
    /**
83
     * Return the HTTP status code.
84
     *
85
     * @return int
86
     */
87
    public function getStatusCode()
88
    {
89
        return $this->statusCode;
90
    }
91
92
    /**
93
     * Return the HTTP headers.
94
     *
95
     * @return array
96
     */
97
    public function getHeaders()
98
    {
99
        return $this->headers;
100
    }
101
102
    /**
103
     * Return the JSON response.
104
     *
105
     * @return array
106
     */
107
    public function json()
108
    {
109
        return $this->decodedBody;
110
    }
111
112
    /**
113
     * Returns true if API returned an error message.
114
     *
115
     * @return boolean
116
     */
117
    public function isError()
118
    {
119
        $excClassName = $this->exc;
120
        return isset($this->decodedBody[$excClassName::ERROR_KEY]);
121
    }
122
123
    /**
124
     * Convert the raw response into an array if possible.
125
     */
126
    public function decodeBody()
127
    {
128
        $this->decodedBody = json_decode($this->body, true);
129
130
        if (!is_array($this->decodedBody)) {
131
            $this->decodedBody = [];
132
        }
133
134
        if ($this->isError()) {
135
            $this->thrownException = new $this->exc($this);
136
        }
137
    }
138
139
    /**
140
     * Returns the exception that was thrown.
141
     *
142
     * @return \Postpay\Exceptions\ApiException|null
143
     */
144
    public function getThrownException()
145
    {
146
        return $this->thrownException;
147
    }
148
}
149