vendor/geocoder-php/common-http/Provider/AbstractHttpProvider.php line 47

Open in your IDE?
  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 Psr\Http\Client\ClientInterface;
  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 ClientInterface
  27.      */
  28.     private $client;
  29.     /**
  30.      * @var MessageFactory
  31.      */
  32.     private $messageFactory;
  33.     /**
  34.      * @param ClientInterface     $client
  35.      * @param MessageFactory|null $factory
  36.      */
  37.     public function __construct(ClientInterface $clientMessageFactory $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 ('' === $body) {
  87.             throw InvalidServerResponse::emptyResponse((string) $request->getUri());
  88.         }
  89.         return $body;
  90.     }
  91.     /**
  92.      * Returns the HTTP adapter.
  93.      *
  94.      * @return ClientInterface
  95.      */
  96.     protected function getHttpClient(): ClientInterface
  97.     {
  98.         return $this->client;
  99.     }
  100.     /**
  101.      * @return MessageFactory
  102.      */
  103.     protected function getMessageFactory(): MessageFactory
  104.     {
  105.         return $this->messageFactory;
  106.     }
  107. }