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
unknown
01:28
created

PsrClientConnection::call()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 4
nc 7
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.', 500, $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
            $request = $this->requestFactory->createRequest($method, $url);
94
            $request->getBody()->write($query);
95
96
            return $request;
97
        }
98
99
        return $this->requestFactory->createRequest($method, $url.'?'.$query);
100
    }
101
102
    /**
103
     * @throws ApiException
104
     */
105
    private function parseResponse(ResponseInterface $response): array
106
    {
107
        $array = json_decode($response->getBody()->getContents(), true);
108
109
        if (\is_array($array) && \array_key_exists('error', $array) && \array_key_exists('message', $array)) {
110
            throw new ApiException($array['message'], $array['error']);
111
        }
112
113
        return $array;
114
    }
115
}
116