Completed
Push — master ( a2b906...d5b295 )
by Peter
03:27
created

GuzzleHttp::initClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace PeterColes\Cluster\HttpClients;
4
5
use PeterColes\Cluster\Contracts\HttpClientInterface;
6
use GuzzleHttp\Client;
7
use Exception;
8
9
class GuzzleHttp implements HttpClientInterface
10
{
11
    public $request;
12
13
    /**
14
     * Initialise Guzzle client using headers appropriate to adapter.
15
     *
16
     * @param array $headers
17
     * @return void
18
     */
19
    public function initClient($headers)
20
    {
21
        try {
22
            $this->request = new Client($headers);
23
        } catch (Exception $e) {
24
            echo 'Unable to initialise http client because '.$e->getMessage()."\n";
25
        }
26
    }
27
28
    /**
29
     * Get status code from http response.
30
     *
31
     * @param GuzzleResponse $response
32
     * @return integer
33
     */
34
    public function getStatus($response)
35
    {
36
        return (int) $response->getStatusCode();
37
    }
38
39
    /**
40
     * Get http response body, cast to json and decode.
41
     *
42
     * @param GuzzleHttp\Response object $response
43
     * @return array
44
     */
45
    public function getBody($response)
46
    {
47
        return json_decode((string) $response->getBody());
48
    }
49
}
50