Completed
Pull Request — master (#147)
by
unknown
11:39
created

TicketVoter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 38
rs 10
wmc 8
lcom 0
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 8 3
A voteOnAttribute() 0 15 3
A canEdit() 0 8 2
1
<?php
2
3
namespace AppBundle\Security;
4
5
use AppBundle\Entity\Ticket;
6
use AppBundle\Entity\User;
7
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
8
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
9
10
class TicketVoter extends Voter
11
{
12
    const EDIT = 'edit';
13
14
    protected function supports($attribute, $subject)
15
    {
16
        if (($attribute !== self::EDIT) && (!$subject instanceof Ticket)) {
17
            return false;
18
        }
19
20
        return true;
21
    }
22
23
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
24
    {
25
        $user = $token->getUser();
26
27
        if (!$user instanceof User) {
28
            return false;
29
        }
30
        /** @var Ticket $ticket */
31
        $ticket = $subject;
32
33
        switch ($attribute) {
34
            case self::EDIT:
35
                return $this->canEdit($ticket, $user);
36
        }
37
    }
38
39
    private function canEdit(Ticket $ticket, User $user)
40
    {
41
        if (!$userOrder = $ticket->getUserOrder()) {
42
            return true;
43
        }
44
45
        return $user === $userOrder->getUser();
46
    }
47
}
48