RandomPrioritizer::sort()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
crap 12
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
    public function sort(array $nodes, $currentNodeId)
15
    {
16
        if (count($nodes) === 1) {
17
            return $nodes;
18
        }
19
20
        $nodeIds = array_keys($nodes);
21
        shuffle($nodeIds);
22
23
        $shuffledNodes = [];
24
        foreach ($nodeIds as $nodeId) {
25
            $shuffledNodes[$nodeId] = $nodes[$nodeId];
26
        }
27
28
        return $shuffledNodes;
29
    }
30
}
31