Lines 100.00% 4 / 4
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
AgentStatVoter 100.00% 4 / 4 100.00% 2 / 2 4 100.00% 1 / 1
 supports 100.00% 1 / 1 100.00% 1 / 1 2
 voteOnAttribute 100.00% 3 / 3 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace App\Security\Voter;
6
7use App\Entity\AgentStat;
8use App\Entity\User;
9use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
10use Symfony\Component\Security\Core\Authorization\Voter\Vote;
11use Symfony\Component\Security\Core\Authorization\Voter\Voter;
12
13/**
14 * Users see their own stats only.
15 *
16 * @extends Voter<string, AgentStat>
17 */
18final class AgentStatVoter extends Voter
19{
20    public const string VIEW = 'AGENT_STAT_VIEW';
21
22    #[\Override]
23    protected function supports(string $attribute, mixed $subject): bool
24    {
25        return self::VIEW === $attribute && $subject instanceof AgentStat;
26    }
27
28    #[\Override]
29    protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token, ?Vote $vote = null): bool
30    {
31        $user = $token->getUser();
32
33        return $user instanceof User
34            && $subject->owner->id === $user->id;
35    }
36}