Completed
Pull Request — development (#809)
by
unknown
04:40
created

CachesVoter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 50
loc 50
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A supports() 5 5 3
A voteOnAttribute() 31 31 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Oc\Security\Voter;
4
5
use Oc\Entity\CachesEntity;
6
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
7
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
8
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
9
use Symfony\Component\Security\Core\User\UserInterface; // ??
10
11 View Code Duplication
class CachesVoter extends Voter
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
{
13
    /**
14
     * @var AccessDecisionManagerInterface
15
     */
16
    private $accessDecisionManager;
17
18
    public function __construct(AccessDecisionManagerInterface $accessDecisionManager)
19
    {
20
        $this->accessDecisionManager = $accessDecisionManager;
21
    }
22
23
    protected function supports($attribute, $subject): bool
24
    {
25
        return in_array($attribute, ['CAN_VIEW'])
26
            && ($subject instanceof CachesEntity || $subject === CachesEntity::class);
27
    }
28
29
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
30
    {
31
        $user = $token->getUser();
32
        // if the user is anonymous, do not grant access
33
        if (!$user instanceof UserInterface) {
34
            return false;
35
        }
36
37
        $grantingRoles = [
38
            'ROLE_SUPER_ADMIN',
39
            'ROLE_ADMIN',
40
            'ROLE_SUPPORT_HEAD',
41
            'ROLE_SOCIAL_HEAD',
42
            'ROLE_DEVELOPER_HEAD',
43
        ];
44
45
        foreach ($grantingRoles as $grantingRole) {
46
            if ($this->accessDecisionManager->decide($token, [$grantingRole])) {
47
                return true;
48
            }
49
        }
50
51
        switch ($attribute) {
52
            case 'CAN_VIEW':
53
                // logic to determine if the user can EDIT
54
                // return true or false
55
                break;
56
        }
57
58
        return false;
59
    }
60
}
61