QueueVoterResolver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 43
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A resolveQueueName() 0 8 2
A getVotes() 0 14 3
1
<?php
2
3
namespace MGDigital\BusQue\QueueResolver;
4
5
use MGDigital\BusQue\Exception\QueueResolverException;
6
use MGDigital\BusQue\QueueResolverInterface;
7
8
class QueueVoterResolver implements QueueResolverInterface
9
{
10
11
    /**
12
     * @var QueueVoterInterface[]
13
     */
14
    private $voters = [ ];
15
16 4
    public function __construct(array $voters)
17
    {
18
        array_walk($voters, function (QueueVoterInterface $voter) {
19 4
            $this->voters[ ] = $voter;
20 4
        });
21 4
    }
22
23 2
    public function resolveQueueName($command): string
24
    {
25 2
        $votes = $this->getVotes($command);
26 2
        if (!isset($votes[ 0 ])) {
27
            throw new QueueResolverException;
28
        }
29 2
        return $votes[ 0 ]->getQueueName();
30
    }
31
32
    /**
33
     * @param mixed $command
34
     * @return QueueVote[]
35
     */
36 2
    public function getVotes($command): array
37
    {
38 2
        $votes = [ ];
39 2
        foreach ($this->voters as $voter) {
40 2
            $vote = $voter->getVote($command);
41 2
            if ($vote instanceof QueueVote) {
42 2
                $votes[ ] = $vote;
43
            }
44
        }
45 2
        usort($votes, function (QueueVote $voteA, QueueVote $voteB) {
46 2
            return $voteB->getConfidence() <=> $voteA->getConfidence();
47 2
        });
48 2
        return $votes;
49
    }
50
}
51