AbstractApi::head()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\RequestBuilderInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * AbstractApi is used to create and send HTTP requests.
13
 */
14
abstract class AbstractApi implements ApiInterface
15
{
16
    /**
17
     * @var \Http\Client\HttpClient
18
     */
19
    private $httpClient;
20
21
    /**
22
     * @var RequestBuilderInterface
23
     */
24
    private $requestBuilder;
25
26
    /**
27
     * @param \Http\Client\HttpClient                    $httpClient     HTTP client to send requests.
28
     * @param \IBM\Watson\Common\RequestBuilderInterface $requestBuilder Request builder to create requests.
29
     */
30
    public function __construct(
31
        HttpClient $httpClient,
32
        RequestBuilderInterface $requestBuilder
33
    ) {
34
        $this->httpClient = $httpClient;
35
        $this->requestBuilder = $requestBuilder;
36
    }
37
38
    /**
39
     * Create and send GET request.
40
     *
41
     * @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...
42
     * @param array               $params  Query parameters.
43
     * @param array               $headers Query headers.
44
     *
45
     * @return \Psr\Http\Message\ResponseInterface
46
     *
47
     * @throws \Http\Client\Exception
48
     */
49
    protected function get($uri, array $params = [], array $headers = []): ResponseInterface
50
    {
51
        return $this->httpClient->sendRequest(
52
            $this->requestBuilder->create(static::HTTP_METHOD_GET, $uri, $params, $headers)
53
        );
54
    }
55
56
    /**
57
     * Create and send HEAD request.
58
     *
59
     * @param string|UriInterface $uri     API URI.
60
     * @param array               $headers Query headers.
61
     *
62
     * @return \Psr\Http\Message\ResponseInterface
63
     *
64
     * @throws \Http\Client\Exception
65
     */
66
    protected function head($uri, array $headers = []): ResponseInterface
67
    {
68
        return $this->httpClient->sendRequest(
69
            $this->requestBuilder->create(static::HTTP_METHOD_HEAD, $uri, $headers, null)
70
        );
71
    }
72
73
    /**
74
     * Create and send TRACE request.
75
     *
76
     * @param string|UriInterface $uri     API URI.
77
     * @param array               $headers Query headers.
78
     *
79
     * @return \Psr\Http\Message\ResponseInterface
80
     *
81
     * @throws \Http\Client\Exception
82
     */
83
    protected function trace($uri, array $headers = []): ResponseInterface
84
    {
85
        return $this->httpClient->sendRequest(
86
            $this->requestBuilder->create(static::HTTP_METHOD_TRACE, $uri, $headers, null)
87
        );
88
    }
89
90
    /**
91
     * Create and send POST request.
92
     *
93
     * @param string|UriInterface $uri     API URI.
94
     * @param array               $params  Query parameters.
95
     * @param array               $headers Query headers.
96
     *
97
     * @return \Psr\Http\Message\ResponseInterface
98
     *
99
     * @throws \Http\Client\Exception
100
     */
101
    protected function post($uri, array $params = [], array $headers = []): ResponseInterface
102
    {
103
        return $this->postRaw($uri, \http_build_query($params), $headers);
104
    }
105
106
    /**
107
     * Create and send POST request.
108
     *
109
     * @param string|UriInterface                  $uri     API URI.
110
     * @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...
111
     * @param array                                $headers Query headers.
112
     *
113
     * @return \Psr\Http\Message\ResponseInterface
114
     *
115
     * @throws \Http\Client\Exception
116
     */
117
    protected function postRaw($uri, $body, array $headers = []): ResponseInterface
118
    {
119
        return $this->httpClient->sendRequest(
120
            $this->requestBuilder->create(static::HTTP_METHOD_POST, $uri, $headers, $body)
121
        );
122
    }
123
124
    /**
125
     * Create and send PUT request.
126
     *
127
     * @param string|UriInterface $uri     API URI.
128
     * @param array               $params  Query parameters.
129
     * @param array               $headers Query headers.
130
     *
131
     * @return \Psr\Http\Message\ResponseInterface
132
     *
133
     * @throws \Http\Client\Exception
134
     */
135
    protected function put($uri, array $params = [], array $headers = []): ResponseInterface
136
    {
137
        return $this->httpClient->sendRequest(
138
            $this->requestBuilder->create(static::HTTP_METHOD_PUT, $uri, $params, $headers)
139
        );
140
    }
141
142
    /**
143
     * Create and send PATCH request.
144
     *
145
     * @param string|UriInterface                  $uri     API URI.
146
     * @param resource|string|StreamInterface|null $body    Request body.
147
     * @param array                                $headers Query headers.
148
     *
149
     * @return \Psr\Http\Message\ResponseInterface
150
     *
151
     * @throws \Http\Client\Exception
152
     */
153
    protected function patch($uri, $body, array $headers = []): ResponseInterface
154
    {
155
        return $this->httpClient->sendRequest(
156
            $this->requestBuilder->create(static::HTTP_METHOD_PATCH, $uri, $headers, $body)
157
        );
158
    }
159
160
    /**
161
     * Create and send DELETE request.
162
     *
163
     * @param string|UriInterface $uri     API URI.
164
     * @param array               $params  Query parameters.
165
     * @param array               $headers Query headers.
166
     *
167
     * @return \Psr\Http\Message\ResponseInterface
168
     *
169
     * @throws \Http\Client\Exception
170
     */
171
    protected function delete($uri, array $params = [], array $headers = []): ResponseInterface
172
    {
173
        return $this->httpClient->sendRequest(
174
            $this->requestBuilder->create(static::HTTP_METHOD_DELETE, $uri, $params, $headers)
175
        );
176
    }
177
178
    /**
179
     * Create and send OPTIONS request.
180
     *
181
     * @param string|UriInterface $uri     API URI.
182
     * @param array               $params  Query parameters.
183
     * @param array               $headers Query headers.
184
     *
185
     * @return \Psr\Http\Message\ResponseInterface
186
     *
187
     * @throws \Http\Client\Exception
188
     */
189
    protected function options($uri, array $params = [], array $headers = []): ResponseInterface
190
    {
191
        return $this->httpClient->sendRequest(
192
            $this->requestBuilder->create(static::HTTP_METHOD_OPTIONS, $uri, $params, $headers)
193
        );
194
    }
195
}
196