Passed
Branch develop (7f369c)
by Nicolas
04:24
created

ArticleVoter::voteOnAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 3
dl 0
loc 15
ccs 5
cts 7
cp 0.7143
crap 3.2098
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Security\Voter;
4
5
use App\Entity\Article;
6
use App\Entity\User;
7
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
8
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
9
10
/**
11
 * ArticleVoter.
12
 */
13
class ArticleVoter extends Voter
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18 6
    protected function supports($attribute, $subject)
19
    {
20 6
        return in_array($attribute, ['OWNER'], true) && $subject instanceof Article;
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 6
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
27
    {
28
        /** @var Article $subject */
29 6
        $user = $token->getUser();
30
31 6
        if (!$user instanceof User) {
32
            return false;
33
        }
34
35
        switch ($attribute) {
36 6
            case 'OWNER':
37 6
                return $subject->getAuthor()->getId() === $user->getId();
38
        }
39
40
        return false;
41
    }
42
}
43