Passed
Pull Request — develop (#23)
by Adam
01:31
created

AbstractApi   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 193
rs 10
c 0
b 0
f 0
wmc 11

11 Methods

Rating   Name   Duplication   Size   Complexity  
A handleErrors() 0 3 1
A __construct() 0 8 1
A patch() 0 4 1
A postRaw() 0 4 1
A options() 0 4 1
A post() 0 3 1
A trace() 0 4 1
A get() 0 4 1
A put() 0 4 1
A delete() 0 4 1
A head() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace IBM\Watson\Common\Api;
6
7
use Http\Client\HttpClient;
8
use IBM\Watson\Common\Hydrator\HydratorInterface;
9
use IBM\Watson\Common\RequestBuilderInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * AbstractApi is used to create and send HTTP requests.
14
 */
15
abstract class AbstractApi implements ApiInterface
16
{
17
    /**
18
     * @var \Http\Client\HttpClient
19
     */
20
    protected $httpClient;
21
22
    /**
23
     * @var \IBM\Watson\Common\Hydrator\HydratorInterface
24
     */
25
    protected $hydrator;
26
27
    /**
28
     * @var RequestBuilderInterface
29
     */
30
    protected $requestBuilder;
31
32
    /**
33
     * @param \Http\Client\HttpClient                       $httpClient     HTTP client to send requests.
34
     * @param \IBM\Watson\Common\Hydrator\HydratorInterface $hydrator       HydratorInterface to hydrate response.
35
     * @param \IBM\Watson\Common\RequestBuilderInterface    $requestBuilder Request builder to create requests.
36
     */
37
    public function __construct(
38
        HttpClient $httpClient,
39
        HydratorInterface $hydrator,
40
        RequestBuilderInterface $requestBuilder
41
    ) {
42
        $this->httpClient = $httpClient;
43
        $this->hydrator = $hydrator;
44
        $this->requestBuilder = $requestBuilder;
45
    }
46
47
    /**
48
     * Create and send GET request.
49
     *
50
     * @param string|UriInterface $uri     API URI.
0 ignored issues
show
Bug introduced by
The type IBM\Watson\Common\Api\UriInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
51
     * @param array               $params  Query parameters.
52
     * @param array               $headers Query headers.
53
     *
54
     * @return \Psr\Http\Message\ResponseInterface
55
     *
56
     * @throws \Http\Client\Exception
57
     */
58
    protected function get($uri, array $params = [], array $headers = []): ResponseInterface
59
    {
60
        return $this->httpClient->sendRequest(
61
            $this->requestBuilder->create(static::HTTP_METHOD_GET, $uri, $params, $headers)
62
        );
63
    }
64
65
    /**
66
     * Create and send HEAD request.
67
     *
68
     * @param string|UriInterface $uri     API URI.
69
     * @param array               $headers Query headers.
70
     *
71
     * @return \Psr\Http\Message\ResponseInterface
72
     *
73
     * @throws \Http\Client\Exception
74
     */
75
    protected function head($uri, array $headers = []): ResponseInterface
76
    {
77
        return $this->httpClient->sendRequest(
78
            $this->requestBuilder->create(static::HTTP_METHOD_HEAD, $uri, $headers, null)
79
        );
80
    }
81
82
    /**
83
     * Create and send TRACE request.
84
     *
85
     * @param string|UriInterface $uri     API URI.
86
     * @param array               $headers Query headers.
87
     *
88
     * @return \Psr\Http\Message\ResponseInterface
89
     *
90
     * @throws \Http\Client\Exception
91
     */
92
    protected function trace($uri, array $headers = []): ResponseInterface
93
    {
94
        return $this->httpClient->sendRequest(
95
            $this->requestBuilder->create(static::HTTP_METHOD_TRACE, $uri, $headers, null)
96
        );
97
    }
98
99
    /**
100
     * Create and send POST request.
101
     *
102
     * @param string|UriInterface $uri     API URI.
103
     * @param array               $params  Query parameters.
104
     * @param array               $headers Query headers.
105
     *
106
     * @return \Psr\Http\Message\ResponseInterface
107
     *
108
     * @throws \Http\Client\Exception
109
     */
110
    protected function post($uri, array $params = [], array $headers = []): ResponseInterface
111
    {
112
        return $this->postRaw($uri, \http_build_query($params), $headers);
113
    }
114
115
    /**
116
     * Create and send POST request.
117
     *
118
     * @param string|UriInterface                  $uri     API URI.
119
     * @param resource|string|StreamInterface|null $body    Request body.
0 ignored issues
show
Bug introduced by
The type IBM\Watson\Common\Api\StreamInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
120
     * @param array                                $headers Query headers.
121
     *
122
     * @return \Psr\Http\Message\ResponseInterface
123
     *
124
     * @throws \Http\Client\Exception
125
     */
126
    protected function postRaw($uri, $body, array $headers = []): ResponseInterface
127
    {
128
        return $this->httpClient->sendRequest(
129
            $this->requestBuilder->create(static::HTTP_METHOD_POST, $uri, $headers, $body)
130
        );
131
    }
132
133
    /**
134
     * Create and send PUT request.
135
     *
136
     * @param string|UriInterface $uri     API URI.
137
     * @param array               $params  Query parameters.
138
     * @param array               $headers Query headers.
139
     *
140
     * @return \Psr\Http\Message\ResponseInterface
141
     *
142
     * @throws \Http\Client\Exception
143
     */
144
    protected function put($uri, array $params = [], array $headers = []): ResponseInterface
145
    {
146
        return $this->httpClient->sendRequest(
147
            $this->requestBuilder->create(static::HTTP_METHOD_PUT, $uri, $params, $headers)
148
        );
149
    }
150
151
    /**
152
     * Create and send PATCH request.
153
     *
154
     * @param string|UriInterface                  $uri     API URI.
155
     * @param resource|string|StreamInterface|null $body    Request body.
156
     * @param array                                $headers Query headers.
157
     *
158
     * @return \Psr\Http\Message\ResponseInterface
159
     *
160
     * @throws \Http\Client\Exception
161
     */
162
    protected function patch($uri, $body, array $headers = []): ResponseInterface
163
    {
164
        return $this->httpClient->sendRequest(
165
            $this->requestBuilder->create(static::HTTP_METHOD_PATCH, $uri, $headers, $body)
166
        );
167
    }
168
169
    /**
170
     * Create and send DELETE request.
171
     *
172
     * @param string|UriInterface $uri     API URI.
173
     * @param array               $params  Query parameters.
174
     * @param array               $headers Query headers.
175
     *
176
     * @return \Psr\Http\Message\ResponseInterface
177
     *
178
     * @throws \Http\Client\Exception
179
     */
180
    protected function delete($uri, array $params = [], array $headers = []): ResponseInterface
181
    {
182
        return $this->httpClient->sendRequest(
183
            $this->requestBuilder->create(static::HTTP_METHOD_DELETE, $uri, $params, $headers)
184
        );
185
    }
186
187
    /**
188
     * Create and send OPTIONS request.
189
     *
190
     * @param string|UriInterface $uri     API URI.
191
     * @param array               $params  Query parameters.
192
     * @param array               $headers Query headers.
193
     *
194
     * @return \Psr\Http\Message\ResponseInterface
195
     *
196
     * @throws \Http\Client\Exception
197
     */
198
    protected function options($uri, array $params = [], array $headers = []): ResponseInterface
199
    {
200
        return $this->httpClient->sendRequest(
201
            $this->requestBuilder->create(static::HTTP_METHOD_OPTIONS, $uri, $params, $headers)
202
        );
203
    }
204
205
    protected function handleErrors(ResponseInterface $response)
206
    {
207
        return $response->getStatusCode();
208
    }
209
}
210