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

RoleVoter::vote()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
crap 3.0416
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