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

RoundRobinClientPool::chooseHttpClient()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 12
nc 5
nop 0
crap 6
1
<?php
2
3
namespace Http\Client\Common\HttpClientPool;
4
5
use Http\Client\Common\Exception\HttpClientNotFoundException;
6
use Http\Client\Common\HttpClientPool;
7
8
/**
9
 * RoundRobinClientPool will choose the next client in the pool.
10
 *
11
 * @author Joel Wurtz <[email protected]>
12
 */
13
final class RoundRobinClientPool extends HttpClientPool
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18 6
    protected function chooseHttpClient()
19
    {
20 6
        $last = current($this->clientPool);
21
22
        do {
23 6
            $client = next($this->clientPool);
24
25 6
            if (false === $client) {
26 6
                $client = reset($this->clientPool);
27
28 6
                if (false === $client) {
29 1
                    throw new HttpClientNotFoundException('Cannot choose a http client as there is no one present in the pool');
30
                }
31 5
            }
32
33
            // Case when there is only one and the last one has been disabled
34 5
            if ($last === $client && $client->isDisabled()) {
35 1
                throw new HttpClientNotFoundException('Cannot choose a http client as there is no one enabled in the pool');
36
            }
37 5
        } while ($client->isDisabled());
38
39 5
        return $client;
40
    }
41
}
42