GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Client   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Test Coverage

Coverage 96.43%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 22
eloc 74
c 7
b 0
f 0
dl 0
loc 237
ccs 81
cts 84
cp 0.9643
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getCaller() 0 7 2
A patch() 0 8 1
A call() 0 12 2
A get() 0 8 1
A setHttpClient() 0 3 1
A put() 0 8 1
A __construct() 0 4 1
A post() 0 8 1
A addAuthenticationHeaders() 0 3 1
A setLogger() 0 3 1
A postJson() 0 20 2
A logRequest() 0 12 3
A prependBaseUrl() 0 3 1
A logResponse() 0 21 4
1
<?php
2
3
namespace Bokbasen\ApiClient;
4
5
use Psr\Http\Client\ClientInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Bokbasen\Auth\Login;
8
use Psr\Http\Message\StreamInterface;
9
use Psr\Log\LoggerInterface;
10
use Bokbasen\ApiClient\Exceptions\BokbasenApiClientException;
11
12
/**
13
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16
 * A PARTICULAR PURPOSE ARE DISCLAIMED.
17
 * IN NO EVENT SHALL THE COPYRIGHT
18
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 */
26
27
/**
28
 * Generic HTTP client for use against Bokbasen APIs.
29
 *
30
 * @license https://opensource.org/licenses/MIT
31
 */
32
class Client
33
{
34
    /**
35
     * @var LoggerInterface
36
     */
37
    protected $logger;
38
39
    /**
40
     * @var Login
41
     */
42
    protected $login;
43
44
    /**
45
     * @var Caller
46
     */
47
    protected $caller;
48
49
    /**
50
     * @var string
51
     */
52
    protected $baseUrl;
53
54
    /**
55
     * @param Login  $login
56
     * @param string $baseUrl
57
     */
58 21
    public function __construct(Login $login, string $baseUrl)
59
    {
60 21
        $this->login = $login;
61 21
        $this->baseUrl = $baseUrl;
62 21
    }
63
64 21
    protected function getCaller(): Caller
65
    {
66 21
        if (!$this->caller) {
67 21
            $this->caller = new Caller();
68
        }
69
70 21
        return $this->caller;
71
    }
72
73 21
    public function setHttpClient(ClientInterface $httpClient): void
74
    {
75 21
        $this->getCaller()->setHttpClient($httpClient);
76 21
    }
77
78
    /**
79
     * @throws BokbasenApiClientException
80
     */
81 21
    protected function call(string $method, string $path, array $headers = [], ?string $body = null, bool $authenticate = true): ResponseInterface
82
    {
83 21
        $headers = $authenticate ? $this->addAuthenticationHeaders($headers) : $headers;
84 21
        $url = $this->prependBaseUrl($path);
85
86 21
        $this->logRequest($method, $url, $body);
87
88 21
        $response = $this->getCaller()->request($method, $url, $headers, $body);
89
90 18
        $this->logResponse($response);
91
92 18
        return $response;
93
    }
94
95
    /**
96
     * Execute POST request
97
     *
98
     * @param string                               $path
99
     * @param string|StreamInterface|null $body
100
     * @param array                                $headers
101
     * @param bool                                 $authenticate
102
     *
103
     * @return ResponseInterface
104
     *
105
     * @throws BokbasenApiClientException
106
     */
107 9
    public function post(string $path, $body, array $headers = [], bool $authenticate = true): ResponseInterface
108
    {
109 9
        return $this->call(
110 9
            HttpRequestOptions::HTTP_METHOD_POST,
111 6
            $path,
112 6
            $headers,
113 6
            $body,
114 6
            $authenticate
115
        );
116
    }
117
118
    /**
119
     * Execute PUT request
120
     *
121
     * @param string                               $path
122
     * @param string|StreamInterface|null $body
123
     * @param array                                $headers
124
     * @param bool                                 $authenticate
125
     *
126
     * @return ResponseInterface
127
     *
128
     * @throws BokbasenApiClientException
129
     */
130 3
    public function put(string $path, $body, array $headers = [], bool $authenticate = true): ResponseInterface
131
    {
132 3
        return $this->call(
133 3
            HttpRequestOptions::HTTP_METHOD_PUT,
134 2
            $path,
135 2
            $headers,
136 2
            $body,
137 2
            $authenticate
138
        );
139
    }
140
141
    /**
142
     * Execute GET request
143
     *
144
     * @param string                               $path
145
     * @param string|StreamInterface|null $body
146
     * @param array                                $headers
147
     * @param bool                                 $authenticate
148
     *
149
     * @return ResponseInterface
150
     *
151
     * @throws BokbasenApiClientException
152
     */
153 6
    public function get(string $path, array $headers = [], $authenticate = true): ResponseInterface
154
    {
155 6
        return $this->call(
156 6
            HttpRequestOptions::HTTP_METHOD_GET,
157 4
            $path,
158 4
            $headers,
159 6
            null,
160 4
            $authenticate
161
        );
162
    }
163
164
    /**
165
     * Execute PATCH request
166
     *
167
     * @param string                               $path
168
     * @param string|StreamInterface|null $body
169
     * @param array                                $headers
170
     * @param bool                                 $authenticate
171
     *
172
     * @return ResponseInterface
173
     *
174
     * @throws BokbasenApiClientException
175
     */
176 3
    public function patch(string $path, $body, array $headers = [], bool $authenticate = true): ResponseInterface
177
    {
178 3
        return $this->call(
179 3
            HttpRequestOptions::HTTP_METHOD_PATCH,
180 2
            $path,
181 2
            $headers,
182 2
            $body,
183 2
            $authenticate
184
        );
185
    }
186
187
    /**
188
     * Special endpoint for posting json, sets correct content type header and encodes data as json
189
     *
190
     * @throws BokbasenApiClientException
191
     */
192 3
    public function postJson(string $path, array $body, array $headers = [], bool $authenticate = true): ResponseInterface
193
    {
194 3
        $body = json_encode($body);
195
196 3
        if ($body === false) {
197
            throw new BokbasenApiClientException('Unable to convert data to json');
198
        }
199
200 3
        $headers = array_merge(
201
            [
202 3
                'Content-Type' => HttpRequestOptions::CONTENT_TYPE_JSON,
203
            ],
204 2
            $headers
205
        );
206
207 3
        return $this->post(
208 3
            $path,
209 2
            $body,
210 2
            $headers,
211 2
            $authenticate
212
        );
213
    }
214
215
    /**
216
     * @param LoggerInterface $logger
217
     */
218 3
    public function setLogger(LoggerInterface $logger = null): void
219
    {
220 3
        $this->logger = $logger;
221 3
    }
222
223 21
    protected function prependBaseUrl(string $path): string
224
    {
225 21
        return sprintf('%s%s', $this->baseUrl, $path);
226
    }
227
228 21
    protected function addAuthenticationHeaders(array $existingHeaders = []): array
229
    {
230 21
        return array_merge($this->login->getAuthHeadersAsArray(), $existingHeaders);
231
    }
232
233 21
    protected function logRequest(string $method, string $url, ?string $body = null): void
234
    {
235 21
        if ($this->logger) {
236
            $logItem = [
237 3
                'method' => $method,
238 3
                'url' => $url,
239
            ];
240
241 3
            if (!empty($body)) {
242 3
                $logItem['body'] = (string) $body;
243
            }
244 3
            $this->logger->info(json_encode($logItem));
245
        }
246 21
    }
247
248 18
    protected function logResponse(ResponseInterface $response): void
249
    {
250 18
        if ($this->logger) {
251
            $logItem = [
252 3
                'code' => $response->getStatusCode(),
253 3
                'headers' => $response->getHeaders(),
254
            ];
255
256
            try {
257 3
                $body = $response->getBody()->getContents();
258
            } catch (\RuntimeException $e) {
259
                $this->logger->warning('Unable to extract body in logger');
260
            }
261
262 3
            if (!empty($body)) {
263 3
                $logItem['body'] = $body;
264
            }
265
266 3
            $this->logger->info(json_encode($logItem));
267
268 3
            $response->getBody()->rewind();
269
        }
270 18
    }
271
}
272