Completed
Push — master ( 4a282c...f5ece7 )
by Mariano
03:58 queued 01:18
created

RandomPrioritizer::sort()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 9
cts 10
cp 0.9
rs 9.4285
cc 3
eloc 9
nc 3
nop 2
crap 3.009
1
<?php
2
namespace Disque\Connection\Node;
3
4
/**
5
 * This prioritizer advises the Manager to switch nodes randomly
6
 *
7
 * It can be used to test the availability of nodes in a cluster.
8
 */
9
class RandomPrioritizer implements NodePrioritizerInterface
10
{
11
    /**
12
     * @inheritdoc
13
     */
14 1
    public function sort(array $nodes, $currentNodeId)
15
    {
16 1
        if (count($nodes) === 1) {
17
            return $nodes;
18
        }
19
20 1
        $nodeIds = array_keys($nodes);
21 1
        shuffle($nodeIds);
22
23 1
        $shuffledNodes = [];
24 1
        foreach ($nodeIds as $nodeId) {
25 1
            $shuffledNodes[$nodeId] = $nodes[$nodeId];
26 1
        }
27
28 1
        return $shuffledNodes;
29
    }
30
}
31