Completed
Push — master ( fa6950...0ff207 )
by Tobias
04:13
created

AbstractHttpProvider::getMessageFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
23
/**
24
 * @author William Durand <[email protected]>
25
 * @author Tobias Nyholm <[email protected]>
26
 */
27
abstract class AbstractHttpProvider extends AbstractProvider
28
{
29
    /**
30
     * @var HttpClient
31
     */
32
    private $client;
33
34
    /**
35
     * @var MessageFactory
36
     */
37
    private $messageFactory;
38
39
    /**
40
     * @param HttpClient          $client
41
     * @param MessageFactory|null $factory
42
     */
43 1
    public function __construct(HttpClient $client, MessageFactory $factory = null)
44
    {
45 1
        $this->client = $client;
46 1
        $this->messageFactory = $factory ?: MessageFactoryDiscovery::find();
47 1
    }
48
49
    /**
50
     * Get URL and return contents. If content is empty, an exception will be thrown.
51
     *
52
     * @param string $url
53
     *
54
     * @return string
55
     *
56
     * @throws InvalidServerResponse
57
     */
58
    protected function getUrlContents(string $url): string
59
    {
60
        $request = $this->getMessageFactory()->createRequest('GET', $url);
61
        $response = $this->getHttpClient()->sendRequest($request);
62
63
        $statusCode = $response->getStatusCode();
64
        if (401 === $statusCode || 403 === $statusCode) {
65
            throw new InvalidCredentials();
66
        } elseif (429 === $statusCode) {
67
            throw new QuotaExceeded();
68
        } elseif ($statusCode >= 300) {
69
            throw InvalidServerResponse::create($url, $statusCode);
70
        }
71
72
        $body = (string) $response->getBody();
73
        if (empty($body)) {
74
            throw InvalidServerResponse::emptyResponse($url);
75
        }
76
77
        return $body;
78
    }
79
80
    /**
81
     * Returns the HTTP adapter.
82
     *
83
     * @return HttpClient
84
     */
85 1
    protected function getHttpClient(): HttpClient
86
    {
87 1
        return $this->client;
88
    }
89
90
    /**
91
     * @return MessageFactory
92
     */
93
    protected function getMessageFactory(): MessageFactory
94
    {
95
        return $this->messageFactory;
96
    }
97
}
98