DeleteBookVoter::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 App\Entity\Book;
15
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
16
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
17
18
class DeleteBookVoter extends Voter
19
{
20
    public const CAN_DELETE_BOOK = 'CAN_DELETE_BOOK';
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 31
    protected function supports($attribute, $subject)
26
    {
27
        // you only want to vote if the attribute and subject are what you expect
28 31
        return self::CAN_DELETE_BOOK === $attribute && ($subject instanceof Book || null === $subject);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 3
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
35
    {
36
        // our previous business logic indicates that admins can do it regardless
37 3
        foreach ($token->getRoles() as $role) {
38 2
            if (\in_array($role->getRole(), ['ROLE_ADMIN'])) {
39 2
                return true;
40
            }
41
        }
42
43 1
        return false;
44
    }
45
}
46