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
Pull Request — master (#2)
by Harald
03:58
created

Caller::request()   A

Complexity

Conditions 5
Paths 17

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 17
nop 4
crap 5
1
<?php
2
3
namespace Bokbasen\ApiClient;
4
5
use Bokbasen\ApiClient\Exceptions\BokbasenApiClientException;
6
use Http\Discovery\Psr17FactoryDiscovery;
7
use Http\Discovery\Psr18ClientDiscovery;
8
use Psr\Http\Client\ClientExceptionInterface;
9
use Psr\Http\Client\ClientInterface;
10
use Psr\Http\Message\RequestFactoryInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\StreamFactoryInterface;
13
use Psr\Http\Message\StreamInterface;
14
use Psr\Http\Message\UriInterface;
15
16
class Caller
17
{
18
    /**
19
     * @var ClientInterface
20
     */
21
    private $httpClient;
22
23
    /**
24
     * @var RequestFactoryInterface
25
     */
26
    private $requestFactory;
27
28
    /**
29
     * @var StreamFactoryInterface
30
     */
31
    private $streamFactory;
32
33 24
    public function __construct(
34
        ClientInterface $httpClient = null,
35
        RequestFactoryInterface $requestFactory = null,
36
        StreamFactoryInterface $streamFactory = null
37
    ) {
38 24
        $this->httpClient = $httpClient ?: Psr18ClientDiscovery::find();
39 24
        $this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
40 24
        $this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory();
41 24
    }
42
43
    /**
44
     * @param string                               $method
45
     * @param string|UriInterface                  $url
46
     * @param array                                $headers
47
     * @param resource|string|StreamInterface|null $body
48
     *
49
     * @return ResponseInterface
50
     *
51
     * @throws BokbasenApiClientException
52
     */
53 24
    public function request(string $method, $url, array $headers = [], $body = null): ResponseInterface
54
    {
55
        try {
56 24
            $request = $this->requestFactory->createRequest($method, $url);
57
58 24
            if (!empty($headers)) {
59 21
                foreach ($headers as $name => $value) {
60 21
                    $request = $request->withHeader($name, $value);
61
                }
62
            }
63
64 24
            if ($body !== null) {
65 15
                $request = $request->withBody(
66 15
                    $this->streamFactory->createStream($body)
0 ignored issues
show
Bug introduced by
It seems like $body can also be of type resource; however, parameter $content of Psr\Http\Message\StreamF...terface::createStream() does only seem to accept string, 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

66
                    $this->streamFactory->createStream(/** @scrutinizer ignore-type */ $body)
Loading history...
67
                );
68
            }
69
70 24
            return $this->httpClient->sendRequest($request);
71 3
        } catch (ClientExceptionInterface | \Exception $e) {
72 3
            throw new BokbasenApiClientException($e->getMessage(), $e->getCode(), $e);
73
        }
74
    }
75
76 21
    public function setHttpClient(ClientInterface $httpClient): void
77
    {
78 21
        $this->httpClient = $httpClient;
79 21
    }
80
}
81