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

RoutableVoter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 25
ccs 0
cts 10
cp 0
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A voteOnAttribute() 0 11 3
A __construct() 0 4 1
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