TestGroupVoter::vote()   D
last analyzed

Complexity

Conditions 10
Paths 9

Size

Total Lines 41
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 21
cts 21
cp 1
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 20
nc 9
nop 3
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Overwatch\TestBundle\Security;
4
5
use Overwatch\UserBundle\Entity\User;
6
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
7
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
8
use Symfony\Component\Security\Core\Role\Role;
9
10
/**
11
 * TestGroupVoter
12
 */
13
class TestGroupVoter implements VoterInterface
14
{
15
    const VIEW = 'view';
16
    const EDIT = 'edit';
17
    
18 23
    public function supportsAttribute($attribute)
19
    {
20 23
        return in_array($attribute, [
21 23
            self::VIEW,
22 23
            self::EDIT,
23 23
        ]);
24
    }
25
    
26 74
    public function supportsClass($class)
27
    {
28 74
        $supportedClass = 'Overwatch\TestBundle\Entity\TestGroup';
29 74
        return $supportedClass === $class || is_subclass_of($class, $supportedClass);
30
    }
31
    
32 74
    public function vote(TokenInterface $token, $group, array $attributes)
33
    {
34
        // check if class of this object is supported by this voter
35 74
        if (!$this->supportsClass(get_class($group))) {
36 72
            return VoterInterface::ACCESS_ABSTAIN;
37
        }
38
39
        // check if the given attribute is covered by this voter
40 23
        if (!$this->supportsAttribute($attributes[0])) {
41 1
            return VoterInterface::ACCESS_ABSTAIN;
42
        }
43
44
        // get current logged in user
45 22
        $user = $token->getUser();
46
        
47
        //allow the token to have ROLE_SUPER_ADMIN before we check the user, for testing
48 22
        if (in_array(new Role('ROLE_SUPER_ADMIN'), $token->getRoles())) {
49 13
            return VoterInterface::ACCESS_GRANTED;
50
        }
51
        
52
        // make sure there is a user object (i.e. that the user is logged in)
53 11
        if (!$user instanceof User) {
54 9
            return VoterInterface::ACCESS_DENIED;
55
        }
56
57 2
        switch ($attributes[0]) {
58 2
            case self::VIEW:
59 1
                if ($user->hasGroup($group->getName())) {
60 1
                    return VoterInterface::ACCESS_GRANTED;
61
                }
62 1
                break;
63
64 1
            case self::EDIT:
65 1
                if ($user->hasGroup($group->getName()) && $user->hasRole('ROLE_ADMIN')) {
66 1
                    return VoterInterface::ACCESS_GRANTED;
67
                }
68 1
                break;
69 2
        }
70
71 2
        return VoterInterface::ACCESS_DENIED;
72
    }
73
}
74