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.

HTTPlugConnection   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 7
dl 0
loc 90
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A call() 0 16 4
A parseResponse() 0 19 4
A buildParameter() 0 4 1
A buildRequest() 0 15 2
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\SetlistFm\Connection;
13
14
use Core23\SetlistFm\Exception\ApiException;
15
use Core23\SetlistFm\Exception\NotFoundException;
16
use Exception;
17
use Http\Client\Exception as ClientException;
18
use Http\Client\HttpClient;
19
use Http\Message\RequestFactory;
20
use Psr\Http\Message\RequestInterface;
21
use Psr\Http\Message\ResponseInterface;
22
23
final class HTTPlugConnection extends AbstractConnection
24
{
25
    /**
26
     * @var HttpClient
27
     */
28
    private $client;
29
30
    /**
31
     * @var RequestFactory
32
     */
33
    private $requestFactory;
34
35
    /**
36
     * Initialize client.
37
     *
38
     * @param string $uri
39
     */
40
    public function __construct(HttpClient $client, RequestFactory $requestFactory, string $apiKey, string $uri = null)
41
    {
42
        parent::__construct($apiKey, $uri);
43
44
        $this->client         = $client;
45
        $this->requestFactory = $requestFactory;
46
    }
47
48
    public function call(string $method, array $params = [], string $requestMethod = 'GET'): array
49
    {
50
        $request = $this->buildRequest($method, $params, $requestMethod);
51
52
        try {
53
            $response = $this->client->sendRequest($request);
54
55
            return $this->parseResponse($response);
56
        } catch (ApiException | NotFoundException $e) {
57
            throw $e;
58
        } catch (Exception $e) {
59
            throw new ApiException('Technical error occurred.', 500, $e);
60
        } catch (ClientException $e) {
61
            throw new ApiException('Technical error occurred.', $e->getCode(), $e);
62
        }
63
    }
64
65
    /**
66
     * @throws ApiException
67
     * @throws NotFoundException
68
     */
69
    private function parseResponse(ResponseInterface $response): array
70
    {
71
        $content = $response->getBody()->getContents();
72
        $array   = json_decode($content, true);
73
74
        if (JSON_ERROR_NONE !== json_last_error()) {
75
            throw new ApiException('Server did not reply with a valid response.', $response->getStatusCode());
76
        }
77
78
        if (404 === $response->getStatusCode()) {
79
            throw new NotFoundException('Server did not find any entity for the request.');
80
        }
81
82
        if ($response->getStatusCode() >= 400) {
83
            throw new ApiException('Technical error occurred.', $response->getStatusCode());
84
        }
85
86
        return $array;
87
    }
88
89
    /**
90
     * Builds request parameter.
91
     */
92
    private static function buildParameter(array $parameter): string
93
    {
94
        return http_build_query($parameter);
95
    }
96
97
    private function buildRequest(string $method, array $params, string $requestMethod): RequestInterface
98
    {
99
        $data = self::buildParameter($params);
100
101
        $headers = [
102
            'Accept'    => 'application/json',
103
            'x-api-key' => $this->getApiKey(),
104
        ];
105
106
        if ('POST' === $requestMethod) {
107
            return $this->requestFactory->createRequest($requestMethod, $this->getUri().$method, $headers, $data);
108
        }
109
110
        return $this->requestFactory->createRequest($requestMethod, $this->getUri().$method.'?'.$data, $headers);
111
    }
112
}
113