Completed
Pull Request — master (#49)
by Joel
04:06
created

LeastUsedClientPool   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 61.53%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 30
ccs 8
cts 13
cp 0.6153
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B chooseHttpClient() 0 24 4
1
<?php
2
3
namespace Http\Client\Common\HttpClientPool;
4
5
use Http\Client\Common\Exception\HttpClientNotFoundException;
6
use Http\Client\Common\HttpClientPool;
7
use Http\Client\Common\HttpClientPoolItem;
8
9
/**
10
 * LeastUsedClientPool will choose the client with the less current request in the pool.
11
 *
12
 * This strategy is only useful when doing async request
13
 *
14
 * @author Joel Wurtz <[email protected]>
15
 */
16
final class LeastUsedClientPool extends HttpClientPool
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 5
    protected function chooseHttpClient()
22
    {
23
        $clientPool = array_filter($this->clientPool, function (HttpClientPoolItem $clientPoolItem) {
24 4
            return !$clientPoolItem->isDisabled();
25 5
        });
26
27 5
        if (0 === count($clientPool)) {
28 2
            throw new HttpClientNotFoundException('Cannot choose a http client as there is no one present in the pool');
29
        }
30
31 4
        usort($clientPool, function (HttpClientPoolItem $clientA, HttpClientPoolItem $clientB) {
32
            if ($clientA->getSendingRequestCount() === $clientB->getSendingRequestCount()) {
33
                return 0;
34
            }
35
36
            if ($clientA->getSendingRequestCount() < $clientB->getSendingRequestCount()) {
37
                return -1;
38
            }
39
40
            return 1;
41 4
        });
42
43 4
        return reset($clientPool);
44
    }
45
}
46