Response   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 281
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 3
dl 0
loc 281
rs 9.84
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromResponse() 0 4 1
A __construct() 0 5 1
A status() 0 4 1
A isSuccess() 0 4 2
A isOk() 0 4 1
A isRedirect() 0 4 2
A isClientError() 0 4 2
A isServerError() 0 4 1
A response() 0 4 1
A body() 0 4 1
A asString() 0 4 1
A asObject() 0 4 1
A asCollection() 0 4 1
A asArray() 0 4 1
A toStream() 0 8 2
A isJson() 0 4 1
A header() 0 10 2
A normalizeHeaders() 0 6 1
A headers() 0 4 1
A requestUrl() 0 4 2
A executionTime() 0 4 2
A customData() 0 4 1
A info() 0 4 1
A contentType() 0 4 1
A userAgent() 0 4 1
1
<?php
2
3
namespace Gemz\HttpClient;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Symfony\Contracts\HttpClient\ResponseInterface;
8
9
class Response
10
{
11
    /** @var ResponseInterface */
12
    protected $response;
13
14
    /** @var bool */
15
    protected $throwErrors;
16
17
    /**
18
     * @param ResponseInterface $response
19
     * @param bool $throwErrors
20
     *
21
     * @return Response
22
     */
23
    public static function createFromResponse(ResponseInterface $response, $throwErrors = false)
24
    {
25
        return new self($response, $throwErrors);
26
    }
27
28
    /**
29
     * @param ResponseInterface $response
30
     * @param bool $throwErrors
31
     */
32
    public function __construct(ResponseInterface $response, $throwErrors = false)
33
    {
34
        $this->response = $response;
35
        $this->throwErrors = $throwErrors;
36
    }
37
38
    /**
39
     * @return int
40
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
41
     */
42
    public function status(): int
43
    {
44
        return $this->response->getStatusCode();
45
    }
46
47
    /**
48
     * @return bool
49
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
50
     */
51
    public function isSuccess(): bool
52
    {
53
        return $this->status() >= 200 && $this->status() < 300;
54
    }
55
56
    /**
57
     * @return bool
58
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
59
     */
60
    public function isOk(): bool
61
    {
62
        return $this->isSuccess();
63
    }
64
65
    /**
66
     * @return bool
67
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
68
     */
69
    public function isRedirect(): bool
70
    {
71
        return $this->status() >= 300 && $this->status() < 400;
72
    }
73
74
    /**
75
     * @return bool
76
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
77
     */
78
    public function isClientError(): bool
79
    {
80
        return $this->status() >= 400 && $this->status() < 500;
81
    }
82
83
    /**
84
     * @return bool
85
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
86
     */
87
    public function isServerError(): bool
88
    {
89
        return $this->status() >= 500;
90
    }
91
92
    /**
93
     * @return ResponseInterface
94
     */
95
    public function response()
96
    {
97
        return $this->response;
98
    }
99
100
    /**
101
     * @return string
102
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
103
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
104
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
105
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
106
     */
107
    public function body(): string
108
    {
109
        return $this->response->getContent($this->throwErrors);
110
    }
111
112
    /**
113
     * @return string
114
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
115
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
116
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
117
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
118
     */
119
    public function asString(): string
120
    {
121
        return $this->body();
122
    }
123
124
    /**
125
     * @return mixed
126
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
127
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
128
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
129
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
130
     */
131
    public function asObject()
132
    {
133
        return json_decode($this->body());
134
    }
135
136
    /**
137
     * @return \Illuminate\Support\Collection<mixed>
0 ignored issues
show
Documentation introduced by
The doc-type \Illuminate\Support\Collection<mixed> could not be parsed: Expected "|" or "end of type", but got "<" at position 30. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
138
     *
139
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
140
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
141
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
142
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
143
     */
144
    public function asCollection(): Collection
145
    {
146
        return collect($this->asArray());
147
    }
148
149
    /**
150
     * @return mixed
151
     *
152
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
153
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
154
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
155
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
156
     */
157
    public function asArray()
158
    {
159
        return json_decode($this->body(), true);
160
    }
161
162
    /**
163
     * @return resource
164
     */
165
    public function toStream()
166
    {
167
        if (method_exists($this->response, 'toStream')) {
168
            return $this->response->toStream($this->throwErrors);
169
        }
170
171
        throw new \BadMethodCallException('method toStream does not exists');
172
    }
173
174
    /**
175
     * @return bool
176
     *
177
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
178
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
179
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
180
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
181
     */
182
    public function isJson(): bool
183
    {
184
        return $this->contentType() == 'application/json';
185
    }
186
187
    /**
188
     * @param string $header
189
     *
190
     * @return mixed|string
191
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
192
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
193
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
194
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
195
     */
196
    public function header(string $header)
197
    {
198
        $header = Str::lower($header);
199
200
        if (array_key_exists($header, $this->headers())) {
201
            return $this->headers()[$header];
202
        }
203
204
        return '';
205
    }
206
207
    /**
208
     * @param array<mixed> $headers
209
     *
210
     * @return array<String>
211
     */
212
    protected function normalizeHeaders(array $headers)
213
    {
214
        return collect($headers)->transform(function ($item, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
215
            return $item[0];
216
        })->all();
217
    }
218
219
    /**
220
     * @return array<String>
221
     *
222
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
223
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
224
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
225
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
226
     */
227
    public function headers(): array
228
    {
229
        return $this->normalizeHeaders($this->response->getHeaders());
230
    }
231
232
    /**
233
     * @return string
234
     */
235
    public function requestUrl(): string
236
    {
237
        return $this->info()['url'] ?: '';
238
    }
239
240
    /**
241
     * @return float
242
     */
243
    public function executionTime(): float
244
    {
245
        return $this->info()['total_time'] ?: 0.0;
246
    }
247
248
    /**
249
     * @return array|mixed|null
250
     */
251
    public function customData()
252
    {
253
        return $this->info()['user_data'];
254
    }
255
256
    /**
257
     * @return array|mixed|null
258
     */
259
    public function info()
260
    {
261
        return $this->response->getInfo();
262
    }
263
264
    /**
265
     * @return mixed|string
266
     *
267
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
268
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
269
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
270
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
271
     */
272
    public function contentType()
273
    {
274
        return $this->header('content-type');
275
    }
276
277
    /**
278
     * @return mixed|string
279
     *
280
     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
281
     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
282
     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
283
     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
284
     */
285
    public function userAgent()
286
    {
287
        return $this->header('user-agent');
288
    }
289
}
290