Completed
Push — master ( 3f17e5...ec676e )
by Ross
9s
created

HandleCommandVoter::supports()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace League\Tactician\Bundle\Security\Voter;
4
5
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
6
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
7
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
8
9
/**
10
 * Voter for security checks on handling commands.
11
 *
12
 * @author Ron Rademaker
13
 */
14
class HandleCommandVoter extends Voter
15
{
16
    /**
17
     * The decision manager.
18
     *
19
     * @var AccessDecisionManagerInterface
20
     */
21
    private $decisionManager;
22
23
    /**
24
     * Command - Require role mapping
25
     *
26
     * @var array
27
     */
28
    private $commandRoleMapping;
29
30
    /**
31
     * Create a new HandleCommandVoter.
32
     *
33
     * @param AccessDecisionManagerInterface $decisionManager
34
     * @param array $commandRoleMapping
35
     * @param string $defaultRole
0 ignored issues
show
Bug introduced by
There is no parameter named $defaultRole. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
36
     */
37 24
    public function __construct(AccessDecisionManagerInterface $decisionManager, array $commandRoleMapping = [])
38
    {
39 24
        $this->decisionManager = $decisionManager;
40 24
        $this->commandRoleMapping = $commandRoleMapping;
41 24
    }
42
43
    /**
44
     * The voter supports checking handle commands
45
     *
46
     * @param string $attribute
47
     * @param object $subject
48
     * @return bool
49
     */
50 24
    protected function supports($attribute, $subject)
51
    {
52 24
        return $attribute === 'handle' && is_object($subject);
53
    }
54
55
    /**
56
     * Checks if the currently logged on user may handle $subject.
57
     *
58
     * @param type $attribute
59
     * @param type $subject
60
     * @param TokenInterface $token
61
     *
62
     * @return bool
63
     */
64 15
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
65
    {
66 15
        $allowedRoles = $this->getAllowedRoles(get_class($subject));
67
68 15
        if (count($allowedRoles) > 0) {
69 6
            return $this->decisionManager->decide($token, $allowedRoles);
70
        } else {
71
            // default conclusion is access denied
72 9
            return false;
73
        }
74
    }
75
76
    /**
77
     * Gets the roles allowed to handle a command of $type
78
     *
79
     * @param string $type
80
     * @return array
81
     */
82 15
    private function getAllowedRoles($type)
83
    {
84 15
        if (array_key_exists($type, $this->commandRoleMapping)) {
85 6
            return $this->commandRoleMapping[$type];
86
        } else {
87 9
            return [];
88
        }
89
    }
90
}
91