Completed
Pull Request — master (#1)
by one
09:53
created

UpdateBookVoter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 7
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 4 3
A voteOnAttribute() 0 10 3
1
<?php
2
3
/*
4
 * (c) Lukasz D. Tulikowski <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace App\Security\Voter\Book;
13
14
use App\Entity\Book;
15
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
16
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
17
18
class UpdateBookVoter extends Voter
19
{
20
    public const CAN_UPDATE_BOOK = 'CAN_UPDATE_BOOK';
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function supports($attribute, $subject)
26
    {
27
        // you only want to vote if the attribute and subject are what you expect
28
        return self::CAN_UPDATE_BOOK === $attribute && ($subject instanceof Book || null === $subject);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
35
    {
36
        // our previous business logic indicates that mods and admins can do it regardless
37
        foreach ($token->getRoles() as $role) {
38
            if (\in_array($role->getRole(), ['ROLE_MODERATOR', 'ROLE_ADMIN'])) {
39
                return true;
40
            }
41
        }
42
43
        return false;
44
    }
45
}
46