Completed
Pull Request — master (#6)
by Ondrej
02:53
created

RoleVoter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 49
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A vote() 0 11 3
1
<?php
2
namespace SpareParts\Overseer\Voter;
3
4
use SpareParts\Overseer\Context\IIdentityContext;
5
use SpareParts\Overseer\Context\IVotingContext;
6
use SpareParts\Overseer\InvalidArgumentException;
7
use SpareParts\Overseer\VotingDecisionEnum;
8
9
final class RoleVoter implements IVoter
10
{
11
    /**
12
     * @var string[]
13
     */
14
    private $allowedRoles;
15
16
    /**
17
     * @var VotingDecisionEnum
18
     */
19
    private $resultDecision;
20
21
    /**
22
     * @var mixed|null
23
     */
24
    private $reason;
25
26
27
    /**
28
     * RoleVoter constructor.
29
     * @param VotingDecisionEnum $resultDecision
30
     * @param string|string[] $allowedRoles
31
     * @param mixed $reason
32
     */
33 10
    public function __construct(VotingDecisionEnum $resultDecision, $allowedRoles, $reason = null)
34
    {
35 10
        $this->allowedRoles = (array) $allowedRoles;
36 10
        $this->resultDecision = $resultDecision;
37 10
        $this->reason = $reason;
38 10
    }
39
40
41
    /**
42
     * @param mixed $votingSubject
43
     * @param \SpareParts\Overseer\Context\IVotingContext $votingContext
44
     * @return ISingleVoterResult
45
     */
46 10
    public function vote($votingSubject, IVotingContext $votingContext)
47
    {
48 10
        if (!($votingContext instanceof IIdentityContext)) {
49
            throw new InvalidArgumentException('RoleVoter can be used only with specific voting context, implementing IIdentityContext.');
50
        }
51
52 10
        if (array_intersect($votingContext->getRoles(), $this->allowedRoles)) {
53 6
            return new SingleVoterResult($this->resultDecision, $this->reason);
54
        }
55 5
        return null;
56
    }
57
}
58