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 ( 31523c...8f74c0 )
by Christian
03:09
created

HTTPlugConnection::call()   B

Complexity

Conditions 5
Paths 14

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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