CreateBookVoter::supports()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
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