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.
Passed
Push — master ( 58fe0f...56dc2f )
by Harald
02:36
created

Client::logRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
namespace Bokbasen\ApiClient;
4
5
use Http\Client\HttpClient;
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 14
    public function __construct(Login $login, string $baseUrl)
59
    {
60 14
        $this->login = $login;
61 14
        $this->baseUrl = $baseUrl;
62 14
    }
63
64 14
    protected function getCaller(): Caller
65
    {
66 14
        if (!$this->caller) {
67 14
            $this->caller = new Caller();
68
        }
69
70 14
        return $this->caller;
71
    }
72
73 14
    public function setHttpClient(HttpClient $httpClient): void
74
    {
75 14
        $this->getCaller()->setHttpClient($httpClient);
76 14
    }
77
78
    /**
79
     * @throws BokbasenApiClientException
80
     */
81 14
    protected function call(string $method, string $path, array $headers = [], $body = null, bool $authenticate = true): ResponseInterface
82
    {
83 14
        $headers = $authenticate ? $this->addAuthenticationHeaders($headers) : $headers;
84 14
        $url = $this->prependBaseUrl($path);
85
86 14
        $this->logRequest($method, $url, $body);
87
88 14
        return $this->getCaller()->request($method, $url, $headers, $body);
89
    }
90
91
    /**
92
     * Execute POST request
93
     *
94
     * @param string                               $path
95
     * @param resource|string|StreamInterface|null $body
96
     * @param array                                $headers
97
     * @param bool                                 $authenticate
98
     *
99
     * @return ResponseInterface
100
     *
101
     * @throws BokbasenApiClientException
102
     */
103 6
    public function post(string $path, $body, ?array $headers = [], bool $authenticate = true): ResponseInterface
104
    {
105 6
        return $this->call(
106 6
            HttpRequestOptions::HTTP_METHOD_POST,
107 6
            $path,
108 6
            $headers,
0 ignored issues
show
Bug introduced by
It seems like $headers can also be of type null; however, parameter $headers of Bokbasen\ApiClient\Client::call() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
            /** @scrutinizer ignore-type */ $headers,
Loading history...
109 6
            $body,
110 6
            $authenticate
111
        );
112
    }
113
114
    /**
115
     * Execute PUT request
116
     *
117
     * @param string                               $path
118
     * @param resource|string|StreamInterface|null $body
119
     * @param array                                $headers
120
     * @param bool                                 $authenticate
121
     *
122
     * @return ResponseInterface
123
     *
124
     * @throws BokbasenApiClientException
125
     */
126 2
    public function put(string $path, $body, array $headers = [], bool $authenticate = true): ResponseInterface
127
    {
128 2
        return $this->call(
129 2
            HttpRequestOptions::HTTP_METHOD_PUT,
130 2
            $path,
131 2
            $headers,
132 2
            $body,
133 2
            $authenticate
134
        );
135
    }
136
137
    /**
138
     * Execute GET request
139
     *
140
     * @param string                               $path
141
     * @param resource|string|StreamInterface|null $body
142
     * @param array                                $headers
143
     * @param bool                                 $authenticate
144
     *
145
     * @return ResponseInterface
146
     *
147
     * @throws BokbasenApiClientException
148
     */
149 4
    public function get(string $path, array $headers = [], $authenticate = true): ResponseInterface
150
    {
151 4
        return $this->call(
152 4
            HttpRequestOptions::HTTP_METHOD_GET,
153 4
            $path,
154 4
            $headers,
155 4
            null,
156 4
            $authenticate
157
        );
158
    }
159
160
    /**
161
     * Execute PATCH request
162
     *
163
     * @param string                               $path
164
     * @param resource|string|StreamInterface|null $body
165
     * @param array                                $headers
166
     * @param bool                                 $authenticate
167
     *
168
     * @return ResponseInterface
169
     *
170
     * @throws BokbasenApiClientException
171
     */
172 2
    public function patch(string $path, $body, array $headers = [], bool $authenticate = true): ResponseInterface
173
    {
174 2
        return $this->call(
175 2
            HttpRequestOptions::HTTP_METHOD_PATCH,
176 2
            $path,
177 2
            $headers,
178 2
            $body,
179 2
            $authenticate
180
        );
181
    }
182
183
    /**
184
     * Special endpoint for posting json, sets correct content type header and encodes data as json
185
     *
186
     * @param string $path
187
     * @param array  $body
188
     *
189
     * @return ResponseInterface
190
     *
191
     * @throws BokbasenApiClientException
192
     */
193 2
    public function postJson(string $path, array $body): ResponseInterface
194
    {
195 2
        $body = json_encode($body);
196
197 2
        if ($body === false) {
198
            throw new BokbasenApiClientException('Unable to convert data to json');
199
        }
200
201 2
        return $this->post(
202 2
            $path,
203 2
            $body,
204
            [
205 2
                'Content-Type' => HttpRequestOptions::CONTENT_TYPE_JSON,
206
            ]
207
        );
208
    }
209
210
    /**
211
     * @param LoggerInterface $logger
212
     */
213 2
    public function setLogger(LoggerInterface $logger = null): void
214
    {
215 2
        $this->logger = $logger;
216 2
    }
217
218 14
    protected function prependBaseUrl(string $path): string
219
    {
220 14
        return sprintf('%s%s', $this->baseUrl, $path);
221
    }
222
223 14
    protected function addAuthenticationHeaders(array $existingHeaders = []): array
224
    {
225 14
        return array_merge($this->login->getAuthHeadersAsArray(), $existingHeaders);
226
    }
227
228 14
    protected function logRequest(string $method, string $url, $body = null): void
229
    {
230 14
        if ($this->logger) {
231 2
            $message = sprintf('Executing HTTP %s request to %s', $method, $url);
232 2
            if (!empty($body)) {
233 2
                $message .= sprintf(' with data %s', $body);
234
            }
235 2
            $this->logger->debug($message);
236
        }
237
    }
238
}