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

RoutableResourceMetadataFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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\ApiPlatform\Metadata\Resource;
15
16
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
17
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
18
use Silverback\ApiComponentsBundle\Entity\Core\RoutableInterface;
19
use Silverback\ApiComponentsBundle\Security\Voter\AbstractRoutableVoter;
20
21
/**
22
 * @author Daniel West <[email protected]>
23
 */
24
class RoutableResourceMetadataFactory implements ResourceMetadataFactoryInterface
25
{
26
    private ResourceMetadataFactoryInterface $decorated;
27
28
    public function __construct(ResourceMetadataFactoryInterface $decorated)
29
    {
30
        $this->decorated = $decorated;
31
    }
32
33
    public function create(string $resourceClass): ResourceMetadata
34
    {
35
        $resourceMetadata = $this->decorated->create($resourceClass);
36
37
        $refl = new \ReflectionClass($resourceClass);
38
        if (!$refl->implementsInterface(RoutableInterface::class)) {
39
            return $resourceMetadata;
40
        }
41
42
        $itemOps = $resourceMetadata->getItemOperations();
43
        foreach ($itemOps as $name => $config) {
44
            if ($config['security'] ?? null) {
45
                continue;
46
            }
47
            $itemOps[$name]['security'] = sprintf("is_granted('%s', object)", AbstractRoutableVoter::READ_ROUTABLE);
48
        }
49
50
        return $resourceMetadata->withItemOperations($itemOps);
0 ignored issues
show
Bug introduced by
It seems like $itemOps can also be of type null; however, parameter $itemOperations of ApiPlatform\Core\Metadat...a::withItemOperations() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        return $resourceMetadata->withItemOperations(/** @scrutinizer ignore-type */ $itemOps);
Loading history...
51
    }
52
}
53