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

RoutableResourceMetadataFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 27
ccs 0
cts 14
cp 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 18 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\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