Passed
Push — master ( 0d69f0...90ddb2 )
by Daniel
05:49
created

RoutableVoter::voteOnAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 12
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Security\Voter;
15
16
use ApiPlatform\Core\Security\ResourceAccessCheckerInterface;
17
use Silverback\ApiComponentsBundle\Entity\Core\RoutableInterface;
18
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
19
20
/**
21
 * If a routable object does not have a route, implement the security configuration. Usually admin only access.
22
 *
23
 * @author Daniel West <[email protected]>
24
 */
25
final class RoutableVoter extends AbstractRoutableVoter
26
{
27
    private ?string $securityStr;
28
    private ResourceAccessCheckerInterface $resourceAccessChecker;
29
30
    public function __construct(?string $securityStr, ResourceAccessCheckerInterface $resourceAccessChecker)
31
    {
32
        $this->securityStr = $securityStr;
33
        $this->resourceAccessChecker = $resourceAccessChecker;
34
    }
35
36
    /**
37
     * @param RoutableInterface $routable
38
     */
39
    protected function voteOnAttribute(string $attribute, $routable, TokenInterface $token): bool
40
    {
41
        if (!$this->securityStr) {
42
            return true;
43
        }
44
45
        if ($routable->getRoute()) {
46
            return true;
47
        }
48
49
        return $this->resourceAccessChecker->isGranted(\get_class($routable), $this->securityStr);
50
    }
51
}
52