Completed
Pull Request — master (#147)
by
unknown
21:40 queued 11:18
created

TicketVoter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 14
cts 16
cp 0.875
rs 10
c 0
b 0
f 0
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 73
    protected function supports($attribute, $subject)
15
    {
16 73
        if (($attribute !== self::EDIT) && (!$subject instanceof Ticket)) {
17 73
            return false;
18
        }
19
20 1
        return true;
21
    }
22
23 1
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
24
    {
25 1
        $user = $token->getUser();
26
27 1
        if (!$user instanceof User) {
28
            return false;
29
        }
30
        /** @var Ticket $ticket */
31 1
        $ticket = $subject;
32
33
        switch ($attribute) {
34 1
            case self::EDIT:
35 1
                return $this->canEdit($ticket, $user);
36
        }
37
    }
38
39 1
    private function canEdit(Ticket $ticket, User $user)
40
    {
41 1
        if (!$userOrder = $ticket->getUserOrder()) {
42 1
            return true;
43
        }
44
45 1
        return $user === $userOrder->getUser();
46
    }
47
}
48