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.
Completed
Pull Request — master (#41)
by Christian
07:08
created

PsrClientConnection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\LastFm\Connection;
13
14
use Core23\LastFm\Exception\ApiException;
15
use Psr\Http\Client\ClientExceptionInterface;
16
use Psr\Http\Client\ClientInterface;
17
use Psr\Http\Message\RequestFactoryInterface;
18
use Psr\Http\Message\RequestInterface;
19
use Psr\Http\Message\ResponseInterface;
20
21
final class PsrClientConnection implements ConnectionInterface
22
{
23
    /**
24
     * @var ClientInterface
25
     */
26
    private $client;
27
28
    /**
29
     * @var RequestFactoryInterface
30
     */
31
    private $requestFactory;
32
33
    /**
34
     * @var string
35
     */
36
    private $endpoint;
37
38
    /**
39
     * Initialize client.
40
     */
41
    public function __construct(ClientInterface $client, RequestFactoryInterface $messageFactory, string $endpoint = ConnectionInterface::DEFAULT_ENDPOINT)
42
    {
43
        $this->client         = $client;
44
        $this->requestFactory = $messageFactory;
45
        $this->endpoint       = $endpoint;
46
    }
47
48
    public function getPageBody(string $url, array $params = [], string $method = 'GET'): ?string
49
    {
50
        $request  = $this->createRequest($method, $url, $params);
51
52
        try {
53
            $response = $this->client->sendRequest($request);
54
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class Core23\LastFm\Connection\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
55
            throw new ApiException(
56
                sprintf('Error fetching page body for url: %s', (string) $request->getUri()),
57
                $e->getCode(),
58
                $e
59
            );
60
        }
61
62
        if ($response->getStatusCode() >= 400) {
63
            return null;
64
        }
65
66
        return $response->getBody()->getContents();
67
    }
68
69
    public function call(string $url, array $params = [], string $method = 'GET'): array
70
    {
71
        $params  = array_merge($params, ['format' => 'json']);
72
        $request = $this->createRequest($method, $this->endpoint, $params);
73
74
        try {
75
            $response = $this->client->sendRequest($request);
76
77
            // Parse response
78
            return $this->parseResponse($response);
79
        } catch (ApiException $e) {
80
            throw $e;
81
        } catch (\Exception $e) {
82
            throw new ApiException('Technical error occurred.', 500, $e);
83
        } catch (ClientExceptionInterface $e) {
84
            throw new ApiException('Technical error occurred.', $e->getCode(), $e);
85
        }
86
    }
87
88
    private function createRequest(string $method, string $url, array $params): RequestInterface
89
    {
90
        $query = http_build_query($params);
91
92
        if ('POST' === $method) {
93
            return $this->requestFactory->createRequest($method, $url, [], $query);
0 ignored issues
show
Unused Code introduced by
The call to RequestFactoryInterface::createRequest() has too many arguments starting with array().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
94
        }
95
96
        return $this->requestFactory->createRequest($method, $url.'?'.$query);
97
    }
98
99
    /**
100
     * @throws ApiException
101
     */
102
    private function parseResponse(ResponseInterface $response): array
103
    {
104
        $array = json_decode($response->getBody()->getContents(), true);
105
106
        if (\is_array($array) && \array_key_exists('error', $array) && \array_key_exists('message', $array)) {
107
            throw new ApiException($array['message'], $array['error']);
108
        }
109
110
        return $array;
111
    }
112
}
113