Completed
Push — master ( 52b1c2...7b34df )
by Tobias
02:29
created

HttpClientPool::sendAsyncRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Http\Client\Common;
4
5
use Http\Client\Common\Exception\HttpClientNotFoundException;
6
use Http\Client\HttpAsyncClient;
7
use Http\Client\HttpClient;
8
use Psr\Http\Message\RequestInterface;
9
10
/**
11
 * A http client pool allows to send requests on a pool of different http client using a specific strategy (least used,
12
 * round robin, ...).
13
 */
14
abstract class HttpClientPool implements HttpAsyncClient, HttpClient
15
{
16
    /**
17
     * @var HttpClientPoolItem[]
18
     */
19
    protected $clientPool = [];
20
21
    /**
22
     * Add a client to the pool.
23
     *
24
     * @param HttpClient|HttpAsyncClient|HttpClientPoolItem $client
25
     */
26 14
    public function addHttpClient($client)
27
    {
28 14
        if (!$client instanceof HttpClientPoolItem) {
29 10
            $client = new HttpClientPoolItem($client);
30 10
        }
31
32 13
        $this->clientPool[] = $client;
33 13
    }
34
35
    /**
36
     * Return an http client given a specific strategy.
37
     *
38
     * @throws HttpClientNotFoundException When no http client has been found into the pool
39
     *
40
     * @return HttpClientPoolItem Return a http client that can do both sync or async
41
     */
42
    abstract protected function chooseHttpClient();
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 6
    public function sendAsyncRequest(RequestInterface $request)
48
    {
49 6
        return $this->chooseHttpClient()->sendAsyncRequest($request);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 13
    public function sendRequest(RequestInterface $request)
56
    {
57 13
        return $this->chooseHttpClient()->sendRequest($request);
58
    }
59
}
60