AuthorVoter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 23
ccs 6
cts 7
cp 0.8571
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A voteOnAttribute() 0 10 2
A supports() 0 3 3
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