AbstractHttpProvider.php 3.0 KB

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