Completed
Push — master ( 2132b9...0efe7a )
by Roberto
02:52
created

UpDownResponse::getHttpStatusCode()   A

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
     * Check HTTP status code (silent/No exceptions!)
114
     * @return int
115
     */
116
    protected function checkHttpStatusCode(): int
117
    {
118
119
        $this->http_status_code = $this->response->getStatusCode();
120
121
        return $this->http_status_code;
122
    }
123
124
    /**
125
     * @return UpDownResult
126
     */
127
    public function getResult()
128
    {
129
130
        return $this->result;
131
    }
132
133
    /**
134
     * @param UpDownResult $result
135
     *
136
     * @return UpDownResponse
137
     */
138
    public function setResult(UpDownResult $result): UpDownResponse
139
    {
140
141
        $this->result = $result;
142
143
        return $this;
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    public function getArrayResponse(): array
150
    {
151
152
        return $this->array_response;
153
    }
154
155
    /**
156
     * @param array $array_response
157
     *
158
     * @return UpDownResponse
159
     */
160
    public function setArrayResponse(array $array_response): UpDownResponse
161
    {
162
163
        $this->array_response = $array_response;
164
165
        return $this;
166
    }
167
168
    /**
169
     * @return mixed
170
     */
171
    public function getErrorMessage()
172
    {
173
174
        return $this->error_message;
175
    }
176
177
    /**
178
     * @param $error_message
179
     *
180
     * @return UpDownResponse
181
     */
182
    public function setErrorMessage($error_message): UpDownResponse
183
    {
184
185
        $this->error_message = $error_message;
186
187
        return $this;
188
    }
189
190
    /**
191
     * @return int
192
     */
193
    public function getHttpStatusCode(): int
194
    {
195
196
        return intval($this->http_status_code);
197
    }
198
199
    /**
200
     * @param array|null $array_response
201
     *
202
     * @throws ResponseException
203
     */
204
    protected function detectError(?array $array_response = null)
205
    {
206
207
        if (is_null($array_response)) {
208
            throw new ResponseException('Missing "results" in UpDownApi Response');
209
        }
210
211
        if (!empty($array_response['error'])) {
212
            $this->setErrorMessage($array_response['error']);
213
            throw new ResponseException($array_response['error']);
214
        }
215
216
    }
217
218
}