RoutableExtension   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 33
ccs 0
cts 15
cp 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A applyToCollection() 0 22 4
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\Doctrine\Extension\ORM;
15
16
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
17
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface;
20
use Doctrine\ORM\QueryBuilder;
21
use Silverback\ApiComponentsBundle\Entity\Core\RoutableInterface;
22
23
/**
24
 * @author Daniel West <[email protected]>
25
 */
26
class RoutableExtension implements QueryCollectionExtensionInterface
27
{
28
    private ?string $securityStr;
29
    private ResourceAccessCheckerInterface $resourceAccessChecker;
30
31
    public function __construct(?string $securityStr, ResourceAccessCheckerInterface $resourceAccessChecker)
32
    {
33
        $this->securityStr = $securityStr;
34
        $this->resourceAccessChecker = $resourceAccessChecker;
35
    }
36
37
    public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
38
    {
39
        if (!$this->securityStr) {
40
            return;
41
        }
42
43
        $refl = new \ReflectionClass($resourceClass);
44
        if (!$refl->implementsInterface(RoutableInterface::class)) {
45
            return;
46
        }
47
48
        if ($this->resourceAccessChecker->isGranted($resourceClass, $this->securityStr)) {
49
            return;
50
        }
51
52
        // we may want to include pages which are routable - but if they are included in a routable page data with a
53
        // publicly accessible route... should we not be trying to restrict routes further to what the routevoter says?
54
        // .. or the route extension... ??
55
        $alias = $queryBuilder->getRootAliases()[0];
56
        $queryBuilder
57
            ->andWhere(
58
                $queryBuilder->expr()->isNotNull("$alias.route")
59
            );
60
    }
61
}
62