AuthorVoter::voteOnAttribute()   A
last analyzed

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 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Security\Voter;
6
7
use App\Entity\Article;
8
use App\Entity\Comment;
9
use App\Entity\User;
10
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
11
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
12
13
final class AuthorVoter extends Voter
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18 7
    protected function supports(string $attribute, $subject): bool
19
    {
20 7
        return $attribute === 'AUTHOR' && ($subject instanceof Article || $subject instanceof Comment);
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 7
    protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
27
    {
28
        /** @var Article|Comment $subject */
29 7
        $user = $token->getUser();
30
31 7
        if (!$user instanceof User) {
32
            return false;
33
        }
34
35 7
        return $subject->getAuthor()->getId() === $user->getId();
36
    }
37
}
38