Cluster   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 44
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A appendNode() 0 3 1
A getRandomNode() 0 18 4
1
<?php
2
namespace evseevnn\Cassandra;
3
use evseevnn\Cassandra\Cluster\Node;
4
use evseevnn\Cassandra\Exception\ClusterException;
5
6
class Cluster {
7
8
	/**
9
	 * @var array
10
	 */
11
	private $nodes;
12
13
	/**
14
	 * @param array $nodes
15
	 */
16
	public function __construct(array $nodes = []) {
17
		$this->nodes = $nodes;
18
	}
19
20
	/**
21
	 * @param string $host
22
	 */
23
	public function appendNode($host) {
24
		$this->nodes[] = $host;
25
	}
26
27
	/**
28
	 * @return Node
29
	 * @throws Exception\ClusterException
30
	 */
31
	public function getRandomNode() {
32
		if (empty($this->nodes)) throw new ClusterException('Node list is empty.');
33
		$nodeKey = array_rand($this->nodes);
34
		$node = $this->nodes[$nodeKey];
35
		try {
36
			if ((array)$node === $node) {
37
				$node = new Node($nodeKey, $node);
38
				unset($this->nodes[$nodeKey]);
39
			} else {
40
				$node = new Node($node);
41
				unset($this->nodes[$nodeKey]);
42
			}
43
		} catch (\InvalidArgumentException $e) {
44
			trigger_error($e->getMessage());
45
		}
46
47
		return $node;
48
	}
49
}