Completed
Pull Request — master (#4)
by Ondrej
01:56
created

VotingAbilityAwareAssembly   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 64
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B canVoteOn() 0 15 7
1
<?php
2
namespace SpareParts\Overseer\Assembly;
3
4
5
use SpareParts\Overseer\Context\IIdentityContext;
6
use SpareParts\Overseer\Context\IVotingContext;
7
use SpareParts\Overseer\StrategyEnum;
8
use SpareParts\Overseer\Voter\IVoter;
9
10
class VotingAbilityAwareAssembly extends VotingAssembly implements IVotingAbilityAwareAssembly
11
{
12
13
    /**
14
     * @var string
15
     */
16
    protected $subjectClassName;
17
18
    /**
19
     * @var string
20
     */
21
    protected $contextClassName;
22
23
    /**
24
     * @var string[]
25
     */
26
    protected $actionList;
27
28
29
    /**
30
     * @param \SpareParts\Overseer\StrategyEnum $strategy
31
     * @param IVoter[] $voters
32
     * @param string|string[] $actions
33
     * @param string $contextClassName
34
     * @param string $subjectClassName
35
     */
36
    public function __construct(
37
        StrategyEnum $strategy,
38
        array $voters,
39
        $actions = null,
40
        $contextClassName = IIdentityContext::class,
41
        $subjectClassName = null
42
    ) {
43
        parent::__construct($strategy, $voters);
44
45
        $this->actionList = (array) $actions;
46
        $this->subjectClassName = $subjectClassName;
47
        $this->contextClassName = $contextClassName;
48
    }
49
50
51
    /**
52
     * @param string $actionName
53
     * @param object $subject
54
     * @param \SpareParts\Overseer\Context\IVotingContext $context
55
     * @return bool
56
     */
57
    public function canVoteOn($actionName, $subject, IVotingContext $context)
58
    {
59
        if ($this->subjectClassName && !($subject instanceof $this->subjectClassName)) {
60
            return false;
61
        }
62
63
        if ($this->contextClassName && !($context instanceof $this->contextClassName)) {
64
            return false;
65
        }
66
67
        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...
68
            return false;
69
        }
70
        return true;
71
    }
72
73
}
74