|
| 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 <william.durand1@gmail.com> |
| 25 | + * @author Tobias Nyholm <tobias.nyholm@gmail.com> |
| 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 | + public function __construct(HttpClient $client, MessageFactory $factory = null) |
| 44 | + { |
| 45 | + $this->client = $client; |
| 46 | + $this->messageFactory = $factory; |
| 47 | + } |
| 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($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 | + protected function getHttpClient(): HttpClient |
| 86 | + { |
| 87 | + return $this->client; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @return MessageFactory |
| 92 | + */ |
| 93 | + protected function getMessageFactory(): MessageFactory |
| 94 | + { |
| 95 | + if ($this->messageFactory === null) { |
| 96 | + $this->messageFactory = MessageFactoryDiscovery::find(); |
| 97 | + } |
| 98 | + |
| 99 | + return $this->messageFactory; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @param HttpClient $client |
| 104 | + */ |
| 105 | + public function setClient(HttpClient $client) |
| 106 | + { |
| 107 | + $this->client = $client; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param MessageFactory $messageFactory |
| 112 | + */ |
| 113 | + public function setMessageFactory(MessageFactory $messageFactory) |
| 114 | + { |
| 115 | + $this->messageFactory = $messageFactory; |
| 116 | + } |
| 117 | +} |
0 commit comments