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
Pull Request — master (#849)
by
unknown
03:03
created

AbstractHttpProvider::getUrlContents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Http\Provider;
14
15
use Geocoder\Exception\InvalidCredentials;
16
use Geocoder\Exception\InvalidServerResponse;
17
use Geocoder\Exception\QuotaExceeded;
18
use Geocoder\Provider\AbstractProvider;
19
use Http\Message\MessageFactory;
20
use Http\Discovery\MessageFactoryDiscovery;
21
use Http\Client\HttpClient;
22
use Psr\Http\Message\ResponseInterface;
23
24
/**
25
 * @author William Durand <[email protected]>
26
 * @author Tobias Nyholm <[email protected]>
27
 */
28
abstract class AbstractHttpProvider extends AbstractProvider
29
{
30
    /**
31
     * @var HttpClient
32
     */
33
    private $client;
34
35
    /**
36
     * @var MessageFactory
37
     */
38
    private $messageFactory;
39
40
    /**
41
     * @param HttpClient          $client
42
     * @param MessageFactory|null $factory
43
     */
44
    public function __construct(HttpClient $client, MessageFactory $factory = null)
45
    {
46
        $this->client = $client;
47
        $this->messageFactory = $factory ?: MessageFactoryDiscovery::find();
48
    }
49
50
    /**
51
     * Get URL and return contents. If content is empty, an exception will be thrown.
52
     *
53
     * @param string $url
54
     *
55
     * @return string
56
     *
57
     * @throws InvalidServerResponse
58
     */
59
    protected function getUrlContents(string $url): string
60
    {
61
        $request = $this->getMessageFactory()->createRequest('GET', $url);
62
        $response = $this->getHttpClient()->sendRequest($request);
63
64
        return $this->parseHttpResponse($response, $url);
65
    }
66
67
    protected function parseHttpResponse(ResponseInterface $response, string $url): string
68
    {
69
        $statusCode = $response->getStatusCode();
70
        if (401 === $statusCode || 403 === $statusCode) {
71
            throw new InvalidCredentials();
72
        } elseif (429 === $statusCode) {
73
            throw new QuotaExceeded();
74
        } elseif ($statusCode >= 300) {
75
            throw InvalidServerResponse::create($url, $statusCode);
76
        }
77
78
        $body = (string) $response->getBody();
79
        if (empty($body)) {
80
            throw InvalidServerResponse::emptyResponse($url);
81
        }
82
83
        return $body;
84
    }
85
86
    /**
87
     * Returns the HTTP adapter.
88
     *
89
     * @return HttpClient
90
     */
91
    protected function getHttpClient(): HttpClient
92
    {
93
        return $this->client;
94
    }
95
96
    /**
97
     * @return MessageFactory
98
     */
99
    protected function getMessageFactory(): MessageFactory
100
    {
101
        return $this->messageFactory;
102
    }
103
}
104