Completed
Push — develop ( 68a985...a1f82e )
by Adam
10s
created

AbstractApi::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
1
<?php
2
3
namespace IBM\Watson\Common\Api;
4
5
use Http\Client\HttpClient;
6
use IBM\Watson\Common\Exception\InsufficientPrivilegesException;
7
use IBM\Watson\Common\Exception\NotFoundException;
8
use IBM\Watson\Common\Exception\UnknownErrorException;
9
use IBM\Watson\Common\Hydrator\HydratorInterface;
10
use IBM\Watson\Common\Hydrator\NoopHydrator;
11
use IBM\Watson\Common\RequestBuilder;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\StreamInterface;
14
15
/**
16
 * @codeCoverageIgnore
17
 */
18
abstract class AbstractApi
19
{
20
    /**
21
     * @var \Http\Client\HttpClient
22
     */
23
    protected $httpClient;
24
25
    /**
26
     * @var \IBM\Watson\Common\Hydrator\HydratorInterface
27
     */
28
    protected $hydrator;
29
30
    /**
31
     * @var \IBM\Watson\Common\RequestBuilder
32
     */
33
    protected $requestBuilder;
34
35
    /**
36
     * AbstractApi constructor.
37
     *
38
     * @param \Http\Client\HttpClient                       $httpClient
39
     * @param \IBM\Watson\Common\Hydrator\HydratorInterface $hydrator
40
     * @param \IBM\Watson\Common\RequestBuilder             $requestBuilder
41
     */
42
    public function __construct(HttpClient $httpClient, HydratorInterface $hydrator, RequestBuilder $requestBuilder)
43
    {
44
        $this->httpClient = $httpClient;
45
46
        if (!$hydrator instanceof NoopHydrator) {
47
            $this->hydrator = $hydrator;
48
        }
49
50
        $this->requestBuilder = $requestBuilder;
51
    }
52
53
    /**
54
     * Create and send PSR-7 GET request
55
     *
56
     * @param string     $path
57
     * @param array|null $params
58
     * @param array|null $headers
59
     *
60
     * @return \Psr\Http\Message\ResponseInterface
61
     *
62
     * @throws \Http\Client\Exception
63
     * @throws \Exception
64
     */
65
    protected function get($path, array $params = null, array $headers = null)
66
    {
67
        if (null === $params) {
68
            $params = [];
69
        }
70
71
        if (null === $headers) {
72
            $headers = [];
73
        }
74
75
        if (count($params) > 0) {
76
            $path .= '?' . \http_build_query($params);
77
        }
78
79
        return $this->httpClient->sendRequest(
80
            $this->requestBuilder->create('GET', $path, $headers)
81
        );
82
    }
83
84
    /**
85
     * Create and send PSR-7 POST request
86
     *
87
     * @param string     $path
88
     * @param array|null $params
89
     * @param array|null $headers
90
     *
91
     * @return \Psr\Http\Message\ResponseInterface
92
     *
93
     * @throws \Http\Client\Exception
94
     * @throws \Exception
95
     */
96
    protected function post($path, array $params = null, array $headers = null)
97
    {
98
        if (null === $headers) {
99
            $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
100
        }
101
102
        if (null === $params) {
103
            $params = [];
104
        }
105
106
        return $this->postRaw($path, \http_build_query($params), $headers);
107
    }
108
109
    /**
110
     * Create and send PSR-7 POST request
111
     *
112
     * @param string                               $path
113
     * @param resource|string|StreamInterface|null $body
114
     * @param array|null                           $headers
115
     *
116
     * @return \Psr\Http\Message\ResponseInterface
117
     *
118
     * @throws \Http\Client\Exception
119
     * @throws \Exception
120
     */
121
    protected function postRaw($path, $body, array $headers = null)
122
    {
123
        if (null === $headers) {
124
            $headers = [];
125
        }
126
127
        return $this->httpClient->sendRequest(
128
            $this->requestBuilder->create('POST', $path, $headers, $body)
129
        );
130
    }
131
132
    /**
133
     * Create and set PSR-7 PUT request
134
     *
135
     * @param string     $path
136
     * @param array|null $params
137
     * @param array|null $headers
138
     *
139
     * @return \Psr\Http\Message\ResponseInterface
140
     *
141
     * @throws \Http\Client\Exception
142
     * @throws \Exception
143
     */
144 View Code Duplication
    protected function put($path, array $params = null, array $headers = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146
        if (null === $headers) {
147
            $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
148
        }
149
150
        return $this->httpClient->sendRequest(
151
            $this->requestBuilder->create('PUT', $path, $headers, \http_build_query($params))
152
        );
153
    }
154
155
    /**
156
     * Create and send PSR-7 PATCH request
157
     *
158
     * @param string     $path
159
     * @param array|null $params
160
     * @param array|null $headers
161
     *
162
     * @return \Psr\Http\Message\ResponseInterface
163
     *
164
     * @throws \Http\Client\Exception
165
     * @throws \Exception
166
     */
167 View Code Duplication
    protected function patch($path, array $params = null, array $headers = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
    {
169
        if (null === $headers) {
170
            $headers = ['Content-Type' => 'application/json'];
171
        }
172
173
        return $this->httpClient->sendRequest(
174
            $this->requestBuilder->create('PATCH', $path, $headers, \json_encode($params))
175
        );
176
    }
177
178
    /**
179
     * Create and send PSR-7 DELETE request
180
     *
181
     * @param string     $path
182
     * @param array|null $params
183
     * @param array|null $headers
184
     *
185
     * @return \Psr\Http\Message\ResponseInterface
186
     *
187
     * @throws \Http\Client\Exception
188
     * @throws \Exception
189
     */
190 View Code Duplication
    protected function delete($path, array $params = null, array $headers = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
    {
192
        if (null === $headers) {
193
            $headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
194
        }
195
196
        return $this->httpClient->sendRequest(
197
            $this->requestBuilder->create('DELETE', $path, $headers, \http_build_query($params))
198
        );
199
    }
200
201
    /**
202
     * Handle API errors
203
     *
204
     * @param \Psr\Http\Message\ResponseInterface $response
205
     *
206
     * @throws \IBM\Watson\Common\Exception\InsufficientPrivilegesException
207
     * @throws \IBM\Watson\Common\Exception\NotFoundException
208
     * @throws \IBM\Watson\Common\Exception\UnknownErrorException
209
     */
210
    protected function handleErrors(ResponseInterface $response)
211
    {
212
        $body = $response->getBody()->__toString();
213
214
        $content = \json_decode($body, true);
215
        $message = '';
216
        if (JSON_ERROR_NONE === \json_last_error()) {
217
            $message = $content['error'];
218
        }
219
220
        switch ($response->getStatusCode()) {
221
            case 401:
222
                throw new InsufficientPrivilegesException($message);
223
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
224
            case 404:
225
                throw new NotFoundException($message);
226
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
227
            default:
228
                throw new UnknownErrorException($message);
229
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
230
        }
231
    }
232
}
233