Test Failed
Pull Request — main (#139)
by Daniel
16:46
created

RoutableResourceMetadataCollectionFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 39
rs 10
c 1
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B create() 0 30 8
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\Metadata\ApiResource;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\Metadata\Operations;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
21
use Silverback\ApiComponentsBundle\Entity\Core\RoutableInterface;
22
use Silverback\ApiComponentsBundle\Security\Voter\AbstractRoutableVoter;
23
24
/**
25
 * @author Daniel West <[email protected]>
26
 */
27
class RoutableResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
28
{
29
    private ResourceMetadataCollectionFactoryInterface $decorated;
30
31
    public function __construct(ResourceMetadataCollectionFactoryInterface $decorated)
32
    {
33
        $this->decorated = $decorated;
34
    }
35
36
    public function create(string $resourceClass): ResourceMetadataCollection
37
    {
38
        $resourceMetadata = $this->decorated->create($resourceClass);
39
40
        $refl = new \ReflectionClass($resourceClass);
41
        if (!$refl->implementsInterface(RoutableInterface::class)) {
42
            return $resourceMetadata;
43
        }
44
45
        $newResources = [];
46
        /** @var ApiResource $resourceMetadatum */
47
        foreach ($resourceMetadata as $resourceMetadatum) {
48
            $newOperations = [];
49
            $operations = $resourceMetadatum->getOperations();
50
            if ($operations) {
51
                /** @var Operation $operation */
52
                foreach ($operations as $i => $operation) {
53
                    if (
54
                        Operation::METHOD_POST !== $operation->getMethod() &&
0 ignored issues
show
Bug introduced by
The constant ApiPlatform\Metadata\Operation::METHOD_POST was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
55
                        !$operation->isCollection() &&
56
                        !$operation->getSecurity()) {
57
                        $operation = $operation->withSecurity(sprintf("is_granted('%s', object)", AbstractRoutableVoter::READ_ROUTABLE));
58
                    }
59
                    $newOperations[$i] = $operation;
60
                }
61
            }
62
            $newResources[] = $resourceMetadatum->withOperations(new Operations($newOperations));
63
        }
64
65
        return new ResourceMetadataCollection($resourceClass, $newResources);
66
    }
67
}
68