VotingAbilityAwareAssembly::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 1
1
<?php
2
namespace SpareParts\Overseer\Assembly;
3
4
5
use SpareParts\Overseer\Context\IVotingContext;
6
use SpareParts\Overseer\StrategyEnum;
7
use SpareParts\Overseer\Voter\IVoter;
8
9
class VotingAbilityAwareAssembly extends VotingAssembly implements IVotingAbilityAwareAssembly
10
{
11
12
    /**
13
     * @var string
14
     */
15
    protected $subjectClassName;
16
17
    /**
18
     * @var string
19
     */
20
    protected $contextClassName;
21
22
    /**
23
     * @var string[]
24
     */
25
    protected $actionList;
26
27
28
    /**
29
     * @param \SpareParts\Overseer\StrategyEnum $strategy
30
     * @param IVoter[] $voters
31
     * @param string|string[] $actions
32
     * @param string $contextClassName
33
     * @param string $subjectClassName
34
     */
35 8
    public function __construct(
36
        StrategyEnum $strategy,
37
        array $voters,
38
        $actions = null,
39
        $subjectClassName = null,
40
        $contextClassName = null
41
    ) {
42 8
        parent::__construct($strategy, $voters);
43
44 8
        $this->actionList = (array) $actions;
45 8
        $this->subjectClassName = $subjectClassName;
46 8
        $this->contextClassName = $contextClassName;
47 8
    }
48
49
50
    /**
51
     * @param string $actionName
52
     * @param object $subject
53
     * @param \SpareParts\Overseer\Context\IVotingContext $context
54
     * @return bool
55
     */
56 8
    public function canVoteOn($actionName, $subject, IVotingContext $context)
57
    {
58 8
        if ($this->subjectClassName && !($subject instanceof $this->subjectClassName)) {
59
            return false;
60
        }
61
62 8
        if ($this->contextClassName && !($context instanceof $this->contextClassName)) {
63 2
            return false;
64
        }
65
66 6
        if ($this->actionList && !in_array($actionName, $this->actionList)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->actionList of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
67 2
            return false;
68
        }
69 5
        return true;
70
    }
71
72
}
73