Open Journal Systems  3.3.0
vendor/php-http/guzzle6-adapter/src/Client.php
1 <?php
2 
3 declare(strict_types=1);
4 
6 
7 use GuzzleHttp\Client as GuzzleClient;
15 
21 final class Client implements HttpClient, HttpAsyncClient
22 {
26  private $client;
27 
28  public function __construct(?ClientInterface $client = null)
29  {
30  if (!$client) {
31  $client = self::buildClient();
32  }
33 
34  $this->client = $client;
35  }
36 
40  public static function createWithConfig(array $config): Client
41  {
42  return new self(self::buildClient($config));
43  }
44 
48  public function sendRequest(RequestInterface $request): ResponseInterface
49  {
50  $promise = $this->sendAsyncRequest($request);
51 
52  return $promise->wait();
53  }
54 
58  public function sendAsyncRequest(RequestInterface $request)
59  {
60  $promise = $this->client->sendAsync($request);
61 
62  return new Promise($promise, $request);
63  }
64 
68  private static function buildClient(array $config = []): GuzzleClient
69  {
70  $handlerStack = new HandlerStack(\GuzzleHttp\choose_handler());
71  $handlerStack->push(Middleware::prepareBody(), 'prepare_body');
72  $config = array_merge(['handler' => $handlerStack], $config);
73 
74  return new GuzzleClient($config);
75  }
76 }
GuzzleHttp
Definition: vendor/guzzlehttp/guzzle/src/Client.php:2
Http\Client\HttpAsyncClient
Definition: HttpAsyncClient.php:13
Psr\Http\Message\RequestInterface
Definition: vendor/psr/http-message/src/RequestInterface.php:24
Http\Adapter\Guzzle6\Client\sendAsyncRequest
sendAsyncRequest(RequestInterface $request)
Definition: vendor/php-http/guzzle6-adapter/src/Client.php:61
GuzzleHttp\Middleware
Definition: Middleware.php:14
Http\Adapter\Guzzle6\Client\createWithConfig
static createWithConfig(array $config)
Definition: vendor/php-http/guzzle6-adapter/src/Client.php:43
GuzzleHttp\ClientInterface
Definition: vendor/guzzlehttp/guzzle/src/ClientInterface.php:13
Http\Client\HttpClient
Definition: HttpClient.php:13
GuzzleHttp\HandlerStack
Definition: HandlerStack.php:12
Http\Adapter\Guzzle6
Definition: vendor/php-http/guzzle6-adapter/src/Client.php:5
Psr\Http\Message\ResponseInterface
Definition: vendor/psr/http-message/src/ResponseInterface.php:20
Http\Adapter\Guzzle6\Client
Definition: vendor/php-http/guzzle6-adapter/src/Client.php:21
Http\Adapter\Guzzle6\Client\sendRequest
sendRequest(RequestInterface $request)
Definition: vendor/php-http/guzzle6-adapter/src/Client.php:51
Http\Adapter\Guzzle6\Promise
Definition: php-http/guzzle6-adapter/src/Promise.php:18
GuzzleHttp\Client
Definition: vendor/guzzlehttp/guzzle/src/Client.php:26
GuzzleHttp\Middleware\prepareBody
static prepareBody()
Definition: Middleware.php:214
Http\Adapter\Guzzle6\Client\__construct
__construct(?ClientInterface $client=null)
Definition: vendor/php-http/guzzle6-adapter/src/Client.php:31