HandleCommandVoter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 78
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

4 Methods

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