Passed
Push — master ( e8ff21...737e91 )
by Julito
12:00
created

UserRelUserVoter::voteOnAttribute()   C

Complexity

Conditions 13
Paths 12

Size

Total Lines 51
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 13
eloc 27
c 1
b 0
f 1
nc 12
nop 3
dl 0
loc 51
rs 6.6166

How to fix   Long Method    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
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Security\Authorization\Voter;
8
9
use Chamilo\CoreBundle\Entity\User;
10
use Chamilo\CoreBundle\Entity\UserRelUser;
11
use Doctrine\ORM\EntityManagerInterface;
12
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
13
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
14
use Symfony\Component\Security\Core\Security;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
17
class UserRelUserVoter extends Voter
18
{
19
    public const CREATE = 'CREATE';
20
    public const VIEW = 'VIEW';
21
    public const EDIT = 'EDIT';
22
    public const DELETE = 'DELETE';
23
24
    private EntityManagerInterface $entityManager;
25
    private Security $security;
26
27
    public function __construct(
28
        EntityManagerInterface $entityManager,
29
        Security $security
30
    ) {
31
        $this->entityManager = $entityManager;
32
        $this->security = $security;
33
    }
34
35
    protected function supports(string $attribute, $subject): bool
36
    {
37
        $options = [
38
            self::CREATE,
39
            self::VIEW,
40
            self::EDIT,
41
            self::DELETE,
42
        ];
43
44
        // if the attribute isn't one we support, return false
45
        if (!\in_array($attribute, $options, true)) {
46
            return false;
47
        }
48
49
        // only vote on Post objects inside this voter
50
        return $subject instanceof UserRelUser;
51
    }
52
53
    protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
54
    {
55
        /** @var User $user */
56
        $user = $token->getUser();
57
58
        if (!$user instanceof UserInterface) {
59
            return false;
60
        }
61
62
        // Admins have access to everything.
63
        if ($this->security->isGranted('ROLE_ADMIN')) {
64
            return true;
65
        }
66
67
        /** @var UserRelUser $userRelUser */
68
        $userRelUser = $subject;
69
70
        switch ($attribute) {
71
            case self::CREATE:
72
                if ($userRelUser->getUser() === $user) {
73
                    return true;
74
                }
75
76
                break;
77
            case self::EDIT:
78
                if ($userRelUser->getUser() === $user) {
79
                    return true;
80
                }
81
82
                if ($userRelUser->getFriend() === $user &&
83
                    UserRelUser::USER_RELATION_TYPE_FRIEND_REQUEST === $userRelUser->getRelationType()
84
                ) {
85
                    return true;
86
                }
87
88
                break;
89
            case self::VIEW:
90
                return true;
91
            case self::DELETE:
92
                if ($userRelUser->getUser() === $user) {
93
                    return true;
94
                }
95
96
                if ($userRelUser->getFriend() === $user) {
97
                    return true;
98
                }
99
100
                break;
101
        }
102
103
        return false;
104
    }
105
}
106