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
Push — master ( 5a2cd3...356324 )
by
unknown
12s
created

PsrClientConnection   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 6
dl 0
loc 95
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getPageBody() 0 20 3
A call() 0 18 4
A createRequest() 0 13 2
A parseResponse() 0 10 4
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 (ClientExceptionInterface $e) {
55
            throw new ApiException(
56
                sprintf('Error fetching page body for url: %s', (string) $request->getUri()),
57
                500,
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