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 ( 8c4ceb...c9fec1 )
by Christian
06:27
created

HTTPlugConnection::getPageBody()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
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\LastFm\Connection;
11
12
use Core23\LastFm\Exception\ApiException;
13
use Http\Client\HttpClient;
14
use Http\Message\MessageFactory;
15
use Psr\Http\Message\ResponseInterface;
16
17
final class HTTPlugConnection extends AbstractConnection
18
{
19
    /**
20
     * @var HttpClient
21
     */
22
    private $client;
23
24
    /**
25
     * @var MessageFactory
26
     */
27
    private $messageFactory;
28
29
    /**
30
     * Initialize client.
31
     *
32
     * @param HttpClient     $client
33
     * @param MessageFactory $messageFactory
34
     * @param string         $apikey
35
     * @param string         $sharedSecret
36
     * @param string         $uri
37
     */
38
    public function __construct(HttpClient $client, MessageFactory $messageFactory, string $apikey, string $sharedSecret, string $uri = null)
39
    {
40
        parent::__construct($apikey, $sharedSecret, $uri);
41
42
        $this->client         = $client;
43
        $this->messageFactory = $messageFactory;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getPageBody(string $url): ? string
50
    {
51
        $request  = $this->messageFactory->createRequest('GET', $url);
52
        $response = $this->client->sendRequest($request);
53
54
        if ($response->getStatusCode() >= 400) {
55
            return null;
56
        }
57
58
        return $response->getBody()->getContents();
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected function call(array $params, string $requestMethod = 'GET') : array
65
    {
66
        $params  = array_merge($params, array('format' => 'json'));
67
        $data    = $this->buildParameter($params);
68
        $request = $this->messageFactory->createRequest($requestMethod, $this->uri, array(), $data);
69
70
        try {
71
            $response = $this->client->sendRequest($request);
72
73
            // Parse response
74
            return $this->parseResponse($response);
75
        } catch (ApiException $e) {
76
            throw $e;
77
        } catch (\Exception $e) {
78
            throw new ApiException('Technical error occurred.', 500, $e);
79
        }
80
    }
81
82
    /**
83
     * @param ResponseInterface $response
84
     *
85
     * @return array
86
     *
87
     * @throws ApiException
88
     */
89
    private function parseResponse(ResponseInterface $response): array
90
    {
91
        $array = json_decode($response->getBody()->getContents(), true);
92
93
        if (is_array($array) && array_key_exists('error', $array) && array_key_exists('message', $array)) {
94
            throw new ApiException($array['message'], $array['error']);
95
        }
96
97
        return $array;
98
    }
99
100
    /**
101
     * Builds request parameter.
102
     *
103
     * @param array $parameter
104
     *
105
     * @return string
106
     */
107
    private function buildParameter(array $parameter): string
108
    {
109
        return http_build_query($parameter);
110
    }
111
}
112