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 ( 603945...58fe0f )
by Harald
03:00 queued 19s
created

Client::setHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 16
    public function __construct(Login $login, string $baseUrl)
59
    {
60 16
        $this->login = $login;
61 16
        $this->baseUrl = $baseUrl;
62 16
    }
63
64 16
    protected function getCaller(): Caller
65
    {
66 16
        if (!$this->caller) {
67 16
            $this->caller = new Caller();
68
        }
69
70 16
        return $this->caller;
71
    }
72
73 16
    public function setHttpClient(HttpClient $httpClient): void
74
    {
75 16
        $this->getCaller()->setHttpClient($httpClient);
76 16
    }
77
78
    /**
79
     * @throws BokbasenApiClientException
80
     */
81 14
    protected function call(string $method, string $path, $body = null, array $headers = [], bool $authenticate = true): ResponseInterface
82
    {
83 14
        $headers = $authenticate ? $this->addAuthenticationHeaders($headers) : $headers;
84 14
        $url = $this->prependBaseUrl($path);
85
86 14
        if ($this->logger) {
87 2
            $this->logger->debug(sprintf('Executing HTTP %s request to %s with data %s.', $method, $url, $body));
88
        }
89
90 14
        return $this->getCaller()->request($method, $url, $headers, $body);
91
    }
92
93
    /**
94
     * Execute POST request
95
     *
96
     * @param string                               $path
97
     * @param resource|string|StreamInterface|null $body
98
     * @param array                                $headers
99
     * @param bool                                 $authenticate
100
     *
101
     * @return ResponseInterface
102
     *
103
     * @throws BokbasenApiClientException
104
     */
105 4
    public function post(string $path, $body, ?array $headers = [], bool $authenticate = true): ResponseInterface
106
    {
107 4
        return $this->call(
108 4
            HttpRequestOptions::HTTP_METHOD_POST,
109 4
            $path,
110 4
            $body,
111 4
            $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

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