Completed
Push — master ( a3cce1...d6fbcc )
by Marcel
09:39
created

ProfileVoter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 29
ccs 8
cts 10
cp 0.8
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A voteOnAttribute() 0 14 4
A supports() 0 2 1
1
<?php
2
3
namespace App\Security\Voter;
4
5
use App\Entity\User;
6
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
7
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
8
9
class ProfileVoter extends Voter {
10
11
    const CHANGE_PASSWORD = 'change_password';
12
    const USE_2FA = 'use_2fa';
13
14
    /**
15
     * @inheritDoc
16
     */
17 7
    protected function supports($attribute, $subject) {
18 7
        return in_array($attribute, [ static::CHANGE_PASSWORD, static::USE_2FA]);
19
    }
20
21
    /**
22
     * @inheritDoc
23
     */
24 5
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token) {
25 5
        $user = $token->getUser();
26
27 5
        if(!$user instanceof User) {
28
            return false;
29
        }
30
31
        switch($attribute) {
32 5
            case static::CHANGE_PASSWORD:
33 5
            case static::USE_2FA:
34 5
                return $user->canChangePassword();
35
        }
36
37
        throw new \LogicException('This code should not be reached');
38
    }
39
}