Completed
Push — master ( 421969...277b3a )
by Dani
01:04
created

src/Http/Response.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 \Postpay\Http\Request The original request.
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 6
    public function __construct(
54
        Request $request,
55
        $statusCode = null,
56
        array $headers = [],
57
        $body = null
58
    ) {
59 6
        $this->request = $request;
60 6
        $this->statusCode = $statusCode;
61 6
        $this->headers = $headers;
62 6
        $this->body = $body;
63
64 6
        if ($this->request->isGraphQL()) {
65 1
            $this->exc = GraphQLException::class;
66
        } else {
67 5
            $this->exc = RESTfulException::class;
68
        }
69 6
        $this->decodeBody();
70 6
    }
71
72
    /**
73
     * Return the original request that returned this response.
74
     *
75
     * @return \Postpay\Http\Request
76
     */
77 6
    public function getRequest()
78
    {
79 6
        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 6
    public function isError()
118
    {
119 6
        $excClassName = $this->exc;
120 6
        return isset($this->decodedBody[$excClassName::ERROR_KEY]);
121
    }
122
123
    /**
124
     * Convert the raw response into an array if possible.
125
     */
126 6
    public function decodeBody()
127
    {
128 6
        $this->decodedBody = json_decode($this->body, true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($this->body, true) of type * is incompatible with the declared type array of property $decodedBody.

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...
129
130 6
        if (!is_array($this->decodedBody)) {
131 6
            $this->decodedBody = [];
132
        }
133
134 6
        if ($this->isError()) {
135
            $this->thrownException = new $this->exc($this);
136
        }
137 6
    }
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