vendor/florianv/exchanger/src/Service/HttpService.php line 105

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Exchanger.
  5.  *
  6.  * (c) Florian Voutzinos <florian@voutzinos.com>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Exchanger\Service;
  12. use Http\Client\HttpClient;
  13. use Http\Discovery\HttpClientDiscovery;
  14. use Http\Discovery\Psr17FactoryDiscovery;
  15. use Psr\Http\Client\ClientInterface;
  16. use Psr\Http\Message\RequestFactoryInterface;
  17. use Psr\Http\Message\RequestInterface;
  18. use Psr\Http\Message\ResponseInterface;
  19. /**
  20.  * Base class for http based services.
  21.  *
  22.  * @author Florian Voutzinos <florian@voutzinos.com>
  23.  */
  24. abstract class HttpService extends Service
  25. {
  26.     /**
  27.      * The client.
  28.      *
  29.      * @var HttpClient|ClientInterface
  30.      */
  31.     private $httpClient;
  32.     /**
  33.      * The request factory.
  34.      *
  35.      * @var RequestFactoryInterface
  36.      */
  37.     private $requestFactory;
  38.     /**
  39.      * @param HttpClient|ClientInterface|null $httpClient
  40.      * @param RequestFactoryInterface|null    $requestFactory
  41.      * @param array                           $options
  42.      */
  43.     public function __construct($httpClient nullRequestFactoryInterface $requestFactory null, array $options = [])
  44.     {
  45.         if (null === $httpClient) {
  46.             $httpClient HttpClientDiscovery::find();
  47.         } else {
  48.             if (!$httpClient instanceof ClientInterface && !$httpClient instanceof HttpClient) {
  49.                 throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
  50.             }
  51.         }
  52.         $this->httpClient $httpClient;
  53.         $this->requestFactory $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
  54.         parent::__construct($options);
  55.     }
  56.     /**
  57.      * @param string $url
  58.      * @param array  $headers
  59.      *
  60.      * @return \Psr\Http\Message\RequestInterface
  61.      */
  62.     private function buildRequest($url, array $headers = []): RequestInterface
  63.     {
  64.         $request $this->requestFactory->createRequest('GET'$url);
  65.         foreach ($headers as $header => $value) {
  66.             $request $request->withHeader($header$value);
  67.         }
  68.         return $request;
  69.     }
  70.     /**
  71.      * Fetches the content of the given url.
  72.      *
  73.      * @param string $url
  74.      * @param array  $headers
  75.      *
  76.      * @return string
  77.      */
  78.     protected function request($url, array $headers = []): string
  79.     {
  80.         return $this->getResponse($url$headers)->getBody()->__toString();
  81.     }
  82.     /**
  83.      * Fetches the content of the given url.
  84.      *
  85.      * @param string $url
  86.      * @param array  $headers
  87.      *
  88.      * @return \Psr\Http\Message\ResponseInterface
  89.      */
  90.     protected function getResponse($url, array $headers = []): ResponseInterface
  91.     {
  92.         return $this->httpClient->sendRequest($this->buildRequest($url$headers));
  93.     }
  94. }