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)) |
|
|
|
|
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
|
|
|
|
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.