Completed
Push — master ( 31741c...acf815 )
by Benjamin
11:01 queued 06:10
created

TeamVoter::canDelete()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 7
cp 0
rs 10
cc 4
nc 3
nop 2
crap 20
1
<?php
2
namespace Obblm\Core\Security\Voter;
3
4
use Obblm\Core\Entity\Coach;
5
use Obblm\Core\Entity\Team;
6
use LogicException;
7
use Obblm\Core\Security\Roles;
8
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
9
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
10
11
class TeamVoter extends Voter
12
{
13
    // these strings are just invented: you can use anything
14
    const VIEW = 'team.view';
15
    const EDIT = 'team.edit';
16
    const DELETE = 'team.delete';
17
    const MANAGE = 'team.manage';
18
19
    protected function supports(string $attribute, $subject)
20
    {
21
        // if the attribute isn't one we support, return false
22
        if (!in_array($attribute, [self::VIEW, self::EDIT, self::DELETE, self::MANAGE])) {
23
            return false;
24
        }
25
26
        // only vote on `Team` objects
27
        if (!$subject instanceof Team) {
28
            return false;
29
        }
30
31
        return true;
32
    }
33
34
    protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
35
    {
36
        $coach = $token->getUser();
37
38
        if (!$coach instanceof Coach) {
39
            // the user must be logged in; if not, deny access
40
            return false;
41
        }
42
43
        // you know $subject is a Team object, thanks to `supports()`
44
        /** @var Team $team */
45
        $team = $subject;
46
47
        switch ($attribute) {
48
            case self::VIEW:
49
                return $this->canView($team, $coach);
50
            case self::EDIT:
51
                return $this->canEdit($team, $coach);
52
            case self::DELETE:
53
                return $this->canDelete($team, $coach);
54
            case self::MANAGE:
55
                return $this->canManage($team, $coach);
56
        }
57
58
        throw new LogicException('This code should not be reached!');
59
    }
60
61
    private function canView(Team $team, Coach $coach)
62
    {
63
        // if they can edit, they can view
64
        if ($this->canManage($team, $coach)) {
65
            return true;
66
        }
67
        return true;
68
    }
69
70
    private function canDelete(Team $team, Coach $coach)
71
    {
72
        if ($this->canEdit($team, $coach)) {
73
            return true;
74
        }
75
        // this assumes that the Team object has a `getCoach()` method
76
        if ($coach === $team->getCoach() &&
77
            !$team->isLockedByManagment()) {
78
            return true;
79
        }
80
81
        return false;
82
    }
83
84
    private function canEdit(Team $team, Coach $coach)
85
    {
86
        if ($this->canManage($team, $coach)) {
87
            return true;
88
        }
89
        // this assumes that the Team object has a `getCoach()` method
90
        if ($coach === $team->getCoach() &&
91
            !$team->isReady() &&
92
            !$team->isLockedByManagment()) {
93
            return true;
94
        }
95
96
        return false;
97
    }
98
99
    private function canManage(Team $team, Coach $coach)
0 ignored issues
show
Unused Code introduced by
The parameter $team is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
    private function canManage(/** @scrutinizer ignore-unused */ Team $team, Coach $coach)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $coach is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
    private function canManage(Team $team, /** @scrutinizer ignore-unused */ Coach $coach)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        return false;
102
    }
103
}
104