Completed
Push — master ( 53681f...45f1fd )
by Elf
02:31
created

HttpClient::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 8
cp 0.875
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 1
crap 4.0312
1
<?php
2
3
namespace ElfSundae;
4
5
use Closure;
6
use Exception;
7
use GuzzleHttp\Client;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Str;
10
use InvalidArgumentException;
11
use Psr\Http\Message\UriInterface;
12
13
class HttpClient
14
{
15
    /**
16
     * The default request options.
17
     *
18
     * @var array
19
     */
20
    protected static $defaultOptions = [
21
        'connect_timeout' => 5,
22
        'timeout' => 25,
23
    ];
24
25
    /**
26
     * The Guzzle client.
27
     *
28
     * @var \GuzzleHttp\Client
29
     */
30
    protected $client;
31
32
    /**
33
     * The Guzzle response.
34
     *
35
     * @var \GuzzleHttp\Psr7\Response
36
     */
37
    protected $response;
38
39
    /**
40
     * The request options.
41
     *
42
     * @var array
43
     */
44
    protected $options = [];
45
46
    /**
47
     * Indicate whether to catch Guzzle exceptions.
48
     *
49
     * @var bool
50
     */
51
    protected $catchExceptions = true;
52
53
    /**
54
     * Get the default request options.
55
     *
56
     * @return array
57
     */
58 24
    public static function defaultOptions()
59
    {
60 24
        return static::$defaultOptions;
61
    }
62
63
    /**
64
     * Set the default request options.
65
     *
66
     * @param  array  $options
67
     * @return void
68
     */
69 1
    public static function setDefaultOptions(array $options)
70
    {
71 1
        static::$defaultOptions = $options;
72 1
    }
73
74
    /**
75
     * Create a http client instance.
76
     *
77
     * @param  array|string|\Psr\Http\Message\UriInterface  $options  base URI or any request options
78
     *
79
     * @throws \InvalidArgumentException
80
     */
81 23
    public function __construct($options = [])
82
    {
83 23
        if (is_string($options) || $options instanceof UriInterface) {
84 1
            $options = ['base_uri' => $options];
85 22
        } elseif (! is_array($options)) {
86
            throw new InvalidArgumentException('config must be a string, UriInterface, or an array');
87
        }
88
89 23
        $this->client = new Client(
90 23
            $this->options = $options + static::defaultOptions()
91
        );
92 23
    }
93
94
    /**
95
     * Get the Guzzle client instance.
96
     *
97
     * @return \GuzzleHttp\Client
98
     */
99 2
    public function getClient()
100
    {
101 2
        return $this->client;
102
    }
103
104
    /**
105
     * Get whether to catch Guzzle exceptions or not.
106
     *
107
     * @return bool
108
     */
109
    public function areExceptionsCaught()
110
    {
111
        return $this->catchExceptions;
112
    }
113
114
    /**
115
     * Set whether to catch Guzzle exceptions or not.
116
     *
117
     * @param  bool  $catch
118
     * @return $this
119
     */
120
    public function catchExceptions($catch)
121
    {
122
        $this->catchExceptions = (bool) $catch;
123
124
        return $this;
125
    }
126
127
    /**
128
     * Get the request options.
129
     *
130
     * @return array
131
     */
132 9
    public function getOptions()
133
    {
134 9
        return $this->options;
135
    }
136
137
    /**
138
     * Set the request options.
139
     *
140
     * @param  array  $options
141
     * @return $this
142
     */
143
    public function setOptions(array $options)
144
    {
145
        $this->options = $options;
146
147
        return $this;
148
    }
149
150
    /**
151
     * Merge the given options to the request options.
152
     *
153
     * @param  array  $options
154
     * @return $this
155
     */
156
    public function mergeOptions(array $options)
157
    {
158
        return $this->setOptions($options + $this->options);
159
    }
160
161
    /**
162
     * Remove request options using "dot" notation.
163
     *
164
     * @param  string|array|null $key
165
     * @return $this
166
     */
167 16
    public function removeOptions($key = null)
168
    {
169 16
        if (is_null($key)) {
170 16
            $this->options = [];
171
        } else {
172 1
            Arr::forget($this->options, is_array($key) ? $key : func_get_args());
173
        }
174
175 16
        return $this;
176
    }
177
178
    /**
179
     * Get a request option using "dot" notation.
180
     *
181
     * @param  string $key
182
     * @return mixed
183
     */
184 3
    public function getOption($key)
185
    {
186 3
        return Arr::get($this->options, $key);
187
    }
188
189
    /**
190
     * Set a request option using "dot" notation.
191
     *
192
     * @param  string|array  $key
193
     * @param  mixed  $value
194
     * @return $this
195
     */
196 10
    public function option($key, $value = null)
197
    {
198 10
        $keys = is_array($key) ? $key : [$key => $value];
199
200 10
        foreach ($keys as $key => $value) {
201 10
            Arr::set($this->options, $key, $value);
202
        }
203
204 10
        return $this;
205
    }
206
207
    /**
208
     * Set the request header.
209
     *
210
     * @param  string  $name
211
     * @param  mixed  $value
212
     * @return $this
213
     */
214 4
    public function header($name, $value)
215
    {
216 4
        return $this->option('headers.'.$name, $value);
217
    }
218
219
    /**
220
     * Set the request content type.
221
     *
222
     * @param  string  $type
223
     * @return $this
224
     */
225 1
    public function contentType($type)
226
    {
227 1
        return $this->header('Content-Type', $type);
228
    }
229
230
    /**
231
     * Set the request accept type.
232
     *
233
     * @param  string  $type
234
     * @return $this
235
     */
236 2
    public function accept($type)
237
    {
238 2
        return $this->header('Accept', $type);
239
    }
240
241
    /**
242
     * Set the request accept type to JSON.
243
     *
244
     * @return $this
245
     */
246 1
    public function acceptJson()
247
    {
248 1
        return $this->accept('application/json');
249
    }
250
251
    /**
252
     * Specify where the body of a response will be saved.
253
     * Set the "sink" option.
254
     *
255
     * @param  mixed  $dest
256
     * @return $this
257
     */
258 1
    public function saveTo($dest)
259
    {
260 1
        return $this->removeOptions('save_to')->option('sink', $dest);
261
    }
262
263
    /**
264
     * Get the Guzzle response instance.
265
     *
266
     * @return \GuzzleHttp\Psr7\Response|null
267
     */
268 8
    public function getResponse()
269
    {
270 8
        return $this->response;
271
    }
272
273
    /**
274
     * Get data from the response.
275
     *
276
     * @param  string|\Closure  $callback
277
     * @param  array  $parameters
278
     * @param  mixed  $default
279
     * @return mixed
280
     */
281 2
    protected function getResponseData($callback, array $parameters = [], $default = null)
282
    {
283 2
        if ($this->response) {
284 2
            return $callback instanceof Closure
285
                ? $callback($this->response, ...$parameters)
286 2
                : $this->response->$callback(...$parameters);
287
        }
288
289
        return $default;
290
    }
291
292
    /**
293
     * Get the response content.
294
     *
295
     * @return string
296
     */
297 2
    public function getContent()
298
    {
299 2
        return (string) $this->getBody();
0 ignored issues
show
Documentation Bug introduced by
The method getBody does not exist on object<ElfSundae\HttpClient>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
300
    }
301
302
    /**
303
     * Get JSON decoded response content.
304
     *
305
     * @param  bool  $assoc
306
     * @return mixed
307
     */
308 1
    public function getJson($assoc = true)
309
    {
310 1
        if ($this->response) {
311 1
            return json_decode($this->getContent(), $assoc);
312
        }
313
    }
314
315
    /**
316
     * Make request to a URI.
317
     *
318
     * @param  string  $uri
319
     * @param  string  $method
320
     * @param  array  $options
321
     * @return $this
322
     */
323 9
    public function request($uri, $method = 'GET', array $options = [])
324
    {
325
        try {
326 9
            $this->response = $this->client->request(
327 9
                $method, $uri, $options += $this->options
328
            );
329 1
        } catch (Exception $e) {
330 1
            if (! $this->catchExceptions) {
331
                throw $e;
332
            }
333
        }
334
335 9
        return $this;
336
    }
337
338
    /**
339
     * Make request to a URI, expecting JSON content.
340
     *
341
     * @param  string  $uri
342
     * @param  string  $method
343
     * @param  array  $options
344
     * @return $this
345
     */
346 3
    public function requestJson($uri, $method = 'GET', array $options = [])
347
    {
348 3
        Arr::set($options, 'headers.Accept', 'application/json');
349
350 3
        return $this->request($uri, $method, $options);
351
    }
352
353
    /**
354
     * Request the URI and return the response content.
355
     *
356
     * @param  string  $uri
357
     * @param  string  $method
358
     * @param  array  $options
359
     * @return string|null
360
     */
361 1
    public function fetchContent($uri, $method = 'GET', array $options = [])
362
    {
363 1
        return $this->request($uri, $method, $options)->getContent();
364
    }
365
366
    /**
367
     * Request the URI and return the JSON decoded response content.
368
     *
369
     * @param  string  $uri
370
     * @param  string  $method
371
     * @param  array  $options
372
     * @param  bool  $assoc
373
     * @return mixed
374
     */
375 1
    public function fetchJson($uri, $method = 'GET', array $options = [], $assoc = true)
376
    {
377 1
        return $this->requestJson($uri, $method, $options)->getJson($assoc);
378
    }
379
380
    /**
381
     * Get the dynamic response methods.
382
     *
383
     * @return array
384
     */
385 6
    protected function getDynamicResponseMethods()
386
    {
387
        return [
388 6
            'getStatusCode', 'getReasonPhrase', 'getProtocolVersion',
389
            'getHeaders', 'hasHeader', 'getHeader', 'getHeaderLine', 'getBody',
390
        ];
391
    }
392
393
    /**
394
     * Dynamically methods to set request option, send request, or get
395
     * response properties.
396
     *
397
     * @param  string  $method
398
     * @param  array  $args
399
     * @return mixed
400
     */
401 8
    public function __call($method, $args)
402
    {
403
        // Handle magic request methods
404 8
        if (in_array($method, ['get', 'head', 'put', 'post', 'patch', 'delete'])) {
405 2
            if (count($args) < 1) {
406
                throw new InvalidArgumentException('Magic request methods require an URI and optional options array');
407
            }
408
409 2
            $url = $args[0];
410 2
            $options = isset($args[1]) ? $args[1] : [];
411
412 2
            return $this->request($url, $method, $options);
413
        }
414
415 6
        if (in_array($method, $this->getDynamicResponseMethods())) {
416 2
            return $this->getResponseData($method, $args);
417
        }
418
419
        // Handle setting request options
420 4
        return $this->option(Str::snake($method), $args[0]);
421
    }
422
}
423