UpDownResponse::getHttpStatusCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (c) 2019 - present
4
 * updown - UpDownResponse.php
5
 * author: Roberto Belotti - [email protected]
6
 * web : robertobelotti.com, github.com/biscolab
7
 * Initial version created on: 15/2/2019
8
 * MIT license: https://github.com/biscolab/updown-php/blob/master/LICENSE
9
 */
10
11
namespace Biscolab\UpDown\Http;
12
13
use Biscolab\UpDown\Exception\ResponseException;
14
use GuzzleHttp\Psr7\Response;
15
16
/**
17
 * Class UpDownResponse
18
 * @package Biscolab\UpDown\Http
19
 */
20
class UpDownResponse
21
{
22
23
    /**
24
     * @var Response
25
     */
26
    protected $response = null;
27
28
    /**
29
     * @var UpDownResult
30
     */
31
    protected $result = null;
32
33
    /**
34
     * @var string
35
     */
36
    protected $status = null;
37
38
    /**
39
     * @var string
40
     */
41
    protected $error_message = null;
42
43
    /**
44
     * @var array
45
     */
46
    protected $array_response = null;
47
48
    /**
49
     * @var int
50
     */
51
    protected $http_status_code = null;
52
53
    /**
54
     * UpDownResponse constructor.
55
     *
56
     * @param Response $response
57
     */
58
    public function __construct(Response $response)
59
    {
60
61
        $this->setResponse($response);
62
63
        $this->parseResponse();
64
65
        $this->checkHttpStatusCode();
66
    }
67
68
    /**
69
     * @param Response $response
70
     *
71
     * @return UpDownResponse
72
     */
73
    public function setResponse(Response $response): UpDownResponse
74
    {
75
76
        $this->response = $response;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @return UpDownResponse
83
     *
84
     * @throws ResponseException
85
     */
86
    protected function parseResponse(): UpDownResponse
87
    {
88
89
        $json_response = $this->response->getBody()->getContents();
90
        $array_response = $this->toArray($json_response);
91
92
        $this->detectError($array_response);
93
94
        $this->setResult(new UpDownResult($array_response));
95
96
        return $this;
97
    }
98
99
    /**
100
     * @param string $json_response
101
     *
102
     * @return array
103
     */
104
    public function toArray(string $json_response): array
105
    {
106
107
        $this->array_response = json_decode($json_response, true);
108
109
        return $this->array_response;
110
    }
111
112
    /**
113
     * @param array|null $array_response
114
     *
115
     * @throws ResponseException
116
     */
117
    protected function detectError(?array $array_response = null)
118
    {
119
120
        if (is_null($array_response)) {
121
            throw new ResponseException('Missing "results" in UpDownApi Response');
122
        }
123
124
        if (!empty($array_response['error'])) {
125
            $this->setErrorMessage($array_response['error']);
126
            throw new ResponseException($array_response['error']);
127
        }
128
129
    }
130
131
    /**
132
     * Check HTTP status code (silent/No exceptions!)
133
     * @return int
134
     */
135
    protected function checkHttpStatusCode(): int
136
    {
137
138
        $this->http_status_code = $this->response->getStatusCode();
139
140
        return $this->http_status_code;
141
    }
142
143
    /**
144
     * @return UpDownResult
145
     */
146
    public function getResult()
147
    {
148
149
        return $this->result;
150
    }
151
152
    /**
153
     * @param UpDownResult $result
154
     *
155
     * @return UpDownResponse
156
     */
157
    public function setResult(UpDownResult $result): UpDownResponse
158
    {
159
160
        $this->result = $result;
161
162
        return $this;
163
    }
164
165
    /**
166
     * @return array
167
     */
168
    public function getArrayResponse(): array
169
    {
170
171
        return $this->array_response;
172
    }
173
174
    /**
175
     * @param array $array_response
176
     *
177
     * @return UpDownResponse
178
     */
179
    public function setArrayResponse(array $array_response): UpDownResponse
180
    {
181
182
        $this->array_response = $array_response;
183
184
        return $this;
185
    }
186
187
    /**
188
     * @return mixed
189
     */
190
    public function getErrorMessage()
191
    {
192
193
        return $this->error_message;
194
    }
195
196
    /**
197
     * @param $error_message
198
     *
199
     * @return UpDownResponse
200
     */
201
    public function setErrorMessage($error_message): UpDownResponse
202
    {
203
204
        $this->error_message = $error_message;
205
206
        return $this;
207
    }
208
209
    /**
210
     * @return int
211
     */
212
    public function getHttpStatusCode(): int
213
    {
214
215
        return intval($this->http_status_code);
216
    }
217
218
}