Pool::valid()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of mmdtl/autolock.
4
 *
5
 * (c) liulu <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the "LICENSE.md"
8
 * file that was distributed with this source code.
9
 */
10
namespace AutoLock;
11
12
use autolock\Drivers\Driver;
13
14
/**
15
 * Class Pool is using to collect servers.
16
 *
17
 * @package AutoLock
18
 * @author Liu Lu <[email protected]>
19
 * @since 0.1
20
 */
21
22
class Pool implements \Iterator
23
{
24
    /**
25
     * @var array Server
26
     */
27
    private $servers;
28
29
    /**
30
     * @var int
31
     */
32
    private $quorum;
33
34
35
    /**
36
     * Pool constructor.
37
     * @param $serversConfig
38
     * @param Driver $driver
39
     */
40 10
    public function __construct($serversConfig, Driver $driver)
41
    {
42 10
        foreach ($serversConfig as $configDsn) {
43 10
            $this->servers[] = $this->getServer($this->getConfig($configDsn), $driver);
44
        }
45 10
        $this->quorum = (int)min(count($this->servers), (floor(count($this->servers) / 2) + 1));
46 10
    }
47
48
49 10
    protected function getConfig($dsn)
50
    {
51 10
        return new Config($dsn);
52
    }
53
54 10
    protected function getServer(Config $config, Driver $driver)
55
    {
56 10
        return new Server($config, $driver);
57
    }
58
59 8
    public function checkQuorum($n)
60
    {
61 8
        return (int)$n >= $this->quorum;
62
    }
63
64 1
    public function rewind()
65
    {
66 1
        reset($this->servers);
67 1
    }
68
69 1
    public function current()
70
    {
71 1
        return current($this->servers);
72
    }
73
74 1
    public function key()
75
    {
76 1
        return key($this->servers);
77
    }
78
79 1
    public function next()
80
    {
81 1
        next($this->servers);
82 1
    }
83
84 1
    public function valid()
85
    {
86 1
        $key = key($this->servers);
87 1
        $servers = ($key !== NULL && $key !== FALSE);
88 1
        return $servers;
89
    }
90
}
91