Passed
Push — master ( e157d2...b7d807 )
by Julito
12:22
created

MessageVoter::voteOnAttribute()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 38
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 19
nc 11
nop 3
dl 0
loc 38
rs 7.3166
c 1
b 0
f 0

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
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\Message;
10
use Chamilo\CoreBundle\Entity\User;
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 MessageVoter extends Voter
18
{
19
    public const VIEW = 'VIEW';
20
    public const EDIT = 'EDIT';
21
    public const DELETE = 'DELETE';
22
23
    private EntityManagerInterface $entityManager;
24
    private Security $security;
25
26
    public function __construct(
27
        EntityManagerInterface $entityManager,
28
        Security $security
29
    ) {
30
        $this->entityManager = $entityManager;
31
        $this->security = $security;
32
    }
33
34
    protected function supports(string $attribute, $subject): bool
35
    {
36
        $options = [
37
            self::VIEW,
38
            self::EDIT,
39
            self::DELETE,
40
        ];
41
42
        // if the attribute isn't one we support, return false
43
        if (!\in_array($attribute, $options, true)) {
44
            return false;
45
        }
46
47
        // only vote on Post objects inside this voter
48
        return $subject instanceof Message;
49
    }
50
51
    protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
52
    {
53
        /** @var User $user */
54
        $user = $token->getUser();
55
56
        if (!$user instanceof UserInterface) {
57
            return false;
58
        }
59
60
        // Admins have access to everything
61
        if ($this->security->isGranted('ROLE_ADMIN')) {
62
            return true;
63
        }
64
65
        /** @var Message $message */
66
        $message = $subject;
67
68
        switch ($attribute) {
69
            case self::VIEW:
70
                if ($message->getUserReceiver() === $user) {
71
                    return true;
72
                }
73
74
                break;
75
            case self::EDIT:
76
            case self::DELETE:
77
                if ($message->getUserReceiver() === $user && Message::MESSAGE_TYPE_INBOX === $message->getMsgType()) {
78
                    return true;
79
                }
80
81
                if ($message->getUserSender() === $user && Message::MESSAGE_TYPE_OUTBOX === $message->getMsgType()) {
82
                    return true;
83
                }
84
85
                break;
86
        }
87
88
        return false;
89
    }
90
}
91