Completed
Push — master ( cd17f3...38b66e )
by David
01:50 queued 12s
created

HttpClientPool   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 52
ccs 10
cts 12
cp 0.8333
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addHttpClient() 0 14 5
chooseHttpClient() 0 1 ?
A sendAsyncRequest() 0 4 1
A sendRequest() 0 4 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): void
31
    {
32 14
        if (!$client instanceof ClientInterface && !$client instanceof HttpAsyncClient && !$client instanceof HttpClientPoolItem) {
33
            throw new \TypeError(
34
                sprintf('%s::addHttpClient(): Argument #1 ($client) must be of type %s|%s|%s, %s given', self::class, ClientInterface::class, HttpAsyncClient::class, HttpClientPoolItem::class, get_debug_type($client))
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with sprintf('%s::addHttpClie...et_debug_type($client)).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
35
            );
36
        }
37
38 14
        if (!$client instanceof HttpClientPoolItem) {
39 10
            $client = new HttpClientPoolItem($client);
40
        }
41
42 14
        $this->clientPool[] = $client;
43 14
    }
44
45
    /**
46
     * Return an http client given a specific strategy.
47
     *
48
     * @throws HttpClientNotFoundException When no http client has been found into the pool
49
     *
50
     * @return HttpClientPoolItem Return a http client that can do both sync or async
51
     */
52
    abstract protected function chooseHttpClient(): HttpClientPoolItem;
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 6
    public function sendAsyncRequest(RequestInterface $request)
58
    {
59 6
        return $this->chooseHttpClient()->sendAsyncRequest($request);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 14
    public function sendRequest(RequestInterface $request): ResponseInterface
66
    {
67 14
        return $this->chooseHttpClient()->sendRequest($request);
68
    }
69
}
70