These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace IBM\Watson\Common\Api; |
||
4 | |||
5 | use Http\Client\HttpClient; |
||
6 | use IBM\Watson\Common\Exception\Domain\RequestEntityTooLargeException; |
||
7 | use IBM\Watson\Common\Exception\InsufficientPrivilegesException; |
||
8 | use IBM\Watson\Common\Exception\InvalidRequestException; |
||
9 | use IBM\Watson\Common\Exception\NotFoundException; |
||
10 | use IBM\Watson\Common\Exception\UnknownErrorException; |
||
11 | use IBM\Watson\Common\Hydrator\HydratorInterface; |
||
12 | use IBM\Watson\Common\Hydrator\NoopHydrator; |
||
13 | use IBM\Watson\Common\RequestBuilder; |
||
14 | use Psr\Http\Message\ResponseInterface; |
||
15 | use Psr\Http\Message\StreamInterface; |
||
16 | |||
17 | /** |
||
18 | * @codeCoverageIgnore |
||
19 | */ |
||
20 | abstract class AbstractApi |
||
21 | { |
||
22 | /** |
||
23 | * @var \Http\Client\HttpClient |
||
24 | */ |
||
25 | protected $httpClient; |
||
26 | |||
27 | /** |
||
28 | * @var \IBM\Watson\Common\Hydrator\HydratorInterface |
||
29 | */ |
||
30 | protected $hydrator; |
||
31 | |||
32 | /** |
||
33 | * @var \IBM\Watson\Common\RequestBuilder |
||
34 | */ |
||
35 | protected $requestBuilder; |
||
36 | |||
37 | /** |
||
38 | * AbstractApi constructor. |
||
39 | * |
||
40 | * @param \Http\Client\HttpClient $httpClient |
||
41 | * @param \IBM\Watson\Common\Hydrator\HydratorInterface $hydrator |
||
42 | * @param \IBM\Watson\Common\RequestBuilder $requestBuilder |
||
43 | */ |
||
44 | public function __construct(HttpClient $httpClient, HydratorInterface $hydrator, RequestBuilder $requestBuilder) |
||
45 | { |
||
46 | $this->httpClient = $httpClient; |
||
47 | |||
48 | if (!$hydrator instanceof NoopHydrator) { |
||
49 | $this->hydrator = $hydrator; |
||
50 | } |
||
51 | |||
52 | $this->requestBuilder = $requestBuilder; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Create and send PSR-7 GET request |
||
57 | * |
||
58 | * @param string $path |
||
59 | * @param array|null $params |
||
60 | * @param array|null $headers |
||
61 | * |
||
62 | * @return \Psr\Http\Message\ResponseInterface |
||
63 | * |
||
64 | * @throws \Http\Client\Exception |
||
65 | * @throws \Exception |
||
66 | */ |
||
67 | protected function get($path, array $params = null, array $headers = null) |
||
68 | { |
||
69 | if (null === $params) { |
||
70 | $params = []; |
||
71 | } |
||
72 | |||
73 | if (null === $headers) { |
||
74 | $headers = []; |
||
75 | } |
||
76 | |||
77 | if (count($params) > 0) { |
||
78 | $path .= '?' . \http_build_query($params); |
||
79 | } |
||
80 | |||
81 | return $this->httpClient->sendRequest( |
||
82 | $this->requestBuilder->create('GET', $path, $headers) |
||
83 | ); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Create and send PSR-7 POST request |
||
88 | * |
||
89 | * @param string $path |
||
90 | * @param array|null $params |
||
91 | * @param array|null $headers |
||
92 | * |
||
93 | * @return \Psr\Http\Message\ResponseInterface |
||
94 | * |
||
95 | * @throws \Http\Client\Exception |
||
96 | * @throws \Exception |
||
97 | */ |
||
98 | protected function post($path, array $params = null, array $headers = null) |
||
99 | { |
||
100 | if (null === $headers) { |
||
101 | $headers = ['Content-Type' => 'application/x-www-form-urlencoded']; |
||
102 | } |
||
103 | |||
104 | if (null === $params) { |
||
105 | $params = []; |
||
106 | } |
||
107 | |||
108 | return $this->postRaw($path, \http_build_query($params), $headers); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Create and send PSR-7 POST request |
||
113 | * |
||
114 | * @param string $path |
||
115 | * @param array|resource|string|StreamInterface|null $body |
||
116 | * @param array|null $headers |
||
117 | * |
||
118 | * @return \Psr\Http\Message\ResponseInterface |
||
119 | * |
||
120 | * @throws \Http\Client\Exception |
||
121 | * @throws \Exception |
||
122 | */ |
||
123 | protected function postRaw($path, $body, array $headers = null) |
||
124 | { |
||
125 | if (null === $headers) { |
||
126 | $headers = []; |
||
127 | } |
||
128 | |||
129 | return $this->httpClient->sendRequest( |
||
130 | $this->requestBuilder->create('POST', $path, $headers, $body) |
||
131 | ); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Create and set PSR-7 PUT request |
||
136 | * |
||
137 | * @param string $path |
||
138 | * @param array|null $params |
||
139 | * @param array|null $headers |
||
140 | * |
||
141 | * @return \Psr\Http\Message\ResponseInterface |
||
142 | * |
||
143 | * @throws \Http\Client\Exception |
||
144 | * @throws \Exception |
||
145 | */ |
||
146 | View Code Duplication | protected function put($path, array $params = null, array $headers = null) |
|
147 | { |
||
148 | if (null === $headers) { |
||
149 | $headers = ['Content-Type' => 'application/x-www-form-urlencoded']; |
||
150 | } |
||
151 | |||
152 | return $this->httpClient->sendRequest( |
||
153 | $this->requestBuilder->create('PUT', $path, $headers, \http_build_query($params)) |
||
154 | ); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Create and send PSR-7 PATCH request |
||
159 | * |
||
160 | * @param string $path |
||
161 | * @param array|null $params |
||
162 | * @param array|null $headers |
||
163 | * |
||
164 | * @return \Psr\Http\Message\ResponseInterface |
||
165 | * |
||
166 | * @throws \Http\Client\Exception |
||
167 | * @throws \Exception |
||
168 | */ |
||
169 | View Code Duplication | protected function patch($path, array $params = null, array $headers = null) |
|
170 | { |
||
171 | if (null === $headers) { |
||
172 | $headers = ['Content-Type' => 'application/json']; |
||
173 | } |
||
174 | |||
175 | return $this->httpClient->sendRequest( |
||
176 | $this->requestBuilder->create('PATCH', $path, $headers, \json_encode($params)) |
||
177 | ); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Create and send PSR-7 DELETE request |
||
182 | * |
||
183 | * @param string $path |
||
184 | * @param array|null $params |
||
185 | * @param array|null $headers |
||
186 | * |
||
187 | * @return \Psr\Http\Message\ResponseInterface |
||
188 | * |
||
189 | * @throws \Http\Client\Exception |
||
190 | * @throws \Exception |
||
191 | */ |
||
192 | View Code Duplication | protected function delete($path, array $params = null, array $headers = null) |
|
193 | { |
||
194 | if (null === $headers) { |
||
195 | $headers = ['Content-Type' => 'application/x-www-form-urlencoded']; |
||
196 | } |
||
197 | |||
198 | return $this->httpClient->sendRequest( |
||
199 | $this->requestBuilder->create('DELETE', $path, $headers, \http_build_query($params)) |
||
200 | ); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Handle API errors |
||
205 | * |
||
206 | * @param \Psr\Http\Message\ResponseInterface $response |
||
207 | * |
||
208 | * @throws \IBM\Watson\Common\Exception\InsufficientPrivilegesException |
||
209 | * @throws \IBM\Watson\Common\Exception\NotFoundException |
||
210 | * @throws \IBM\Watson\Common\Exception\UnknownErrorException |
||
211 | * @throws \IBM\Watson\Common\Exception\Domain\RequestEntityTooLargeException |
||
212 | * @throws \IBM\Watson\Common\Exception\InvalidRequestException |
||
213 | */ |
||
214 | protected function handleErrors(ResponseInterface $response) |
||
215 | { |
||
216 | $body = $response->getBody()->__toString(); |
||
217 | |||
218 | $content = \json_decode($body, true); |
||
219 | $message = ''; |
||
220 | if (JSON_ERROR_NONE === \json_last_error()) { |
||
221 | $message = $content['error']; |
||
222 | } |
||
223 | |||
224 | switch ($response->getStatusCode()) { |
||
225 | case 400: |
||
226 | throw new InvalidRequestException($message); |
||
227 | case 401: |
||
228 | throw new InsufficientPrivilegesException($message); |
||
229 | break; |
||
0 ignored issues
–
show
|
|||
230 | case 404: |
||
231 | throw new NotFoundException($message); |
||
232 | break; |
||
0 ignored issues
–
show
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 function fx() {
try {
doSomething();
return true;
}
catch (\Exception $e) {
return false;
}
return false;
}
In the above example, the last
Loading history...
|
|||
233 | case 413: |
||
234 | throw new RequestEntityTooLargeException($message); |
||
235 | default: |
||
236 | throw new UnknownErrorException($message); |
||
237 | break; |
||
0 ignored issues
–
show
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 function fx() {
try {
doSomething();
return true;
}
catch (\Exception $e) {
return false;
}
return false;
}
In the above example, the last
Loading history...
|
|||
238 | } |
||
239 | } |
||
240 | } |
||
241 |
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
orexit
statements that have been added for debug purposes.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.