Completed
Push — master ( 50831e...b2bfbe )
by Márk
01:58
created

Client::sendAsyncRequest()   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
    use HttpClientEmulator;
22
23
    /**
24
     * @var ClientInterface
25
     */
26
    private $client;
27
28
    /**
29
     * @param ClientInterface|null $client
30
     */
31 324
    public function __construct(ClientInterface $client = null)
32
    {
33 324
        if (!$client) {
34
            $handlerStack = new HandlerStack(\GuzzleHttp\choose_handler());
35
            $handlerStack->push(Middleware::prepareBody(), 'prepare_body');
36
            $client = new GuzzleClient(['handler' => $handlerStack]);
37
        }
38 324
        $this->client = $client;
39 324
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 324
    public function sendAsyncRequest(RequestInterface $request)
45
    {
46 324
        $promise = $this->client->sendAsync($request);
47
48 324
        return new Promise($promise, $request);
49
    }
50
}
51