Passed
Branch master (1fd71c)
by Nicolas
03:28
created

AuthorVoter::voteOnAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 10
ccs 4
cts 5
cp 0.8
crap 2.032
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\Comment;
7
use App\Entity\User;
8
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
9
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
10
11
/**
12
 * ArticleVoter.
13
 */
14
class AuthorVoter extends Voter
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 19
    protected function supports($attribute, $subject)
20
    {
21 19
        return 'AUTHOR' === $attribute && ($subject instanceof Article || $subject instanceof Comment);
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 7
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
28
    {
29
        /** @var Article|Comment $subject */
30 7
        $user = $token->getUser();
31
32 7
        if (!$user instanceof User) {
33
            return false;
34
        }
35
36 7
        return $subject->getAuthor()->getId() === $user->getId();
37
    }
38
}
39