CreateBookVoter::voteOnAttribute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 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 Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
16
17
class CreateBookVoter extends Voter
18
{
19
    public const CAN_CREATE_BOOK = 'CAN_CREATE_BOOK';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 33
    protected function supports($attribute, $subject)
25
    {
26
        // you only want to vote if the attribute and subject are what you expect
27 33
        return self::CAN_CREATE_BOOK === $attribute && null === $subject;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 3
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
34
    {
35
        // our previous business logic indicates that mods and admins can do it regardless
36 3
        foreach ($token->getRoles() as $role) {
37 2
            if (\in_array($role->getRole(), ['ROLE_MODERATOR', 'ROLE_ADMIN'])) {
38 2
                return true;
39
            }
40
        }
41
42 1
        return false;
43
    }
44
}
45