Passed
Push — master ( 145379...16ef67 )
by Marcelo
06:15
created

Json::getJsonContentFromResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Brofist\ApiClient;
4
5
use GuzzleHttp\Client as HttpClient;
6
use GuzzleHttp\Exception\RequestException;
7
use Psr\Http\Message\ResponseInterface;
8
9
class Json implements JsonInterface
10
{
11
    /**
12
     * @var HttpClient
13
     */
14
    private $httpClient;
15
16
    /**
17
     * @var string
18
     */
19
    private $endpoint = '';
20
21
    /**
22
     * @var array
23
     */
24
    private $additionalOptions = [];
25
26
    /**
27
     * @param array $options
28
     */
29 12
    public function __construct(array $options = [])
30
    {
31 12
        if (!isset($options['endpoint'])) {
32 1
            throw new \InvalidArgumentException("Endpoint not set");
33
        }
34
35 12
        $this->endpoint = trim($options['endpoint'], '/');
36
37 12
        if (isset($options['authToken'])) {
38 1
            $this->additionalOptions['auth'] = [$options['authToken'], ''];
39 1
        }
40
41 12
        if (isset($options['basicAuth'])) {
42 1
            $this->additionalOptions['auth'] = $options['basicAuth'];
43 1
        }
44
45 12
        if (!isset($options['httpClient'])) {
46 1
            $options['httpClient'] = new HttpClient();
47 1
        }
48
49 12
        $this->httpClient = $options['httpClient'];
50 12
    }
51
52
    /**
53
     * @return string
54
     */
55 1
    public function getEndpoint()
56
    {
57 1
        return $this->endpoint;
58
    }
59
60 5
    public function get($path, array $params = [])
61
    {
62 5
        return $this->request('GET', $path, ['query' => $params]);
63
    }
64
65 1
    public function post($path, array $postData = [])
66
    {
67 1
        return $this->request('POST', $path, ['form_params' => $postData]);
68
    }
69
70 1
    public function put($path, array $putData = [])
71
    {
72 1
        return $this->request('PUT', $path, ['json' => $putData]);
73
    }
74
75 1
    public function delete($endpoint, array $data = [])
76
    {
77 1
        throw new \BadMethodCallException("Not implemented yet");
78
    }
79
80 1
    public function patch($endpoint, array $data = [])
81
    {
82 1
        throw new \BadMethodCallException("Not implemented yet");
83
    }
84
85
    /**
86
     * @param string $method
87
     * @param string $path
88
     * @param array  $options
89
     *
90
     * @throws InvalidJsonResponseBodyException
91
     * @throws Exception
92
     *
93
     * @return array
94
     */
95 7
    private function request($method, $path, $options)
96
    {
97
        try {
98 7
            $uri = $this->endpoint . $path;
99 7
            $options = array_merge($options, $this->additionalOptions);
100 7
            $response = $this->httpClient->request($method, $uri, $options);
101
102 6
            return $this->getJsonContentFromResponse($response);
103 2
        } catch (RequestException $e) {
104 1
            $message = $e->getMessage();
105 1
            $data = json_decode($e->getResponse()->getBody(), true);
106
107 1
            if (isset($data['message'])) {
108 1
                $message = $data['message'];
109 1
            }
110
111 1
            throw new Exception($message, null, $e);
112
        }
113
    }
114
115
    /**
116
     * @param ResponseInterface $response
117
     *
118
     * @throws InvalidJsonResponseBodyException
119
     *
120
     * @return array
121
     */
122 6
    private function getJsonContentFromResponse(ResponseInterface $response)
123
    {
124 6
        $body = $response->getBody();
125 6
        $content = json_decode($body, true);
126
127 6
        if (JSON_ERROR_NONE !== json_last_error()) {
128 1
            throw new InvalidJsonResponseBodyException($body);
129
        }
130
131 5
        return $content;
132
    }
133
}
134