Completed
Push — master ( 306b11...65fd35 )
by Márk
05:18
created

Client::sendRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Http\Adapter\Guzzle6;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Middleware;
9
use Http\Client\HttpAsyncClient;
10
use Http\Client\HttpClient;
11
use Http\Client\Common\HttpClientEmulator;
12
use Psr\Http\Message\RequestInterface;
13
14
/**
15
 * HTTP Adapter for Guzzle 6.
16
 *
17
 * @author David de Boer <[email protected]>
18
 */
19
class Client implements HttpClient, HttpAsyncClient
20
{
21
    /**
22
     * @var ClientInterface
23
     */
24
    private $client;
25
26
    /**
27
     * @param ClientInterface|null $client
28
     */
29 324
    public function __construct(ClientInterface $client = null)
30
    {
31 324
        if (!$client) {
32
            $handlerStack = new HandlerStack(\GuzzleHttp\choose_handler());
33
            $handlerStack->push(Middleware::prepareBody(), 'prepare_body');
34
            $client = new GuzzleClient(['handler' => $handlerStack]);
35
        }
36 324
        $this->client = $client;
37 324
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 159
    public function sendRequest(RequestInterface $request)
43
    {
44 159
        $promise = $this->sendAsyncRequest($request);
45
46 159
        return $promise->wait();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 324
    public function sendAsyncRequest(RequestInterface $request)
53
    {
54 324
        $promise = $this->client->sendAsync($request);
55
56 324
        return new Promise($promise, $request);
57
    }
58
}
59