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 ( 0ceb01...daa4a9 )
by Christian
01:47
created

HTTPlugConnection::call()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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