Completed
Push — master ( 9c21b6...43c791 )
by David
04:48
created

HttpClientPool::sendRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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