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

RoutingPrefixResourceMetadataCollectionFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 65
rs 10
c 1
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A prefixRoute() 0 31 5
A create() 0 23 5
A __construct() 0 3 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\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\AbstractComponent;
22
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData;
23
24
/**
25
 * @author Daniel West <[email protected]>
26
 */
27
class RoutingPrefixResourceMetadataCollectionFactory 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
        $routePrefixParts = [];
41
        if (is_subclass_of($resourceClass, AbstractComponent::class)) {
42
            $routePrefixParts[] = 'component';
43
        } elseif (is_subclass_of($resourceClass, AbstractPageData::class)) {
44
            $routePrefixParts[] = 'page_data';
45
        } else {
46
            // underscores for core resources
47
            $reflection = new \ReflectionClass($resourceClass);
48
            $namespace = $reflection->getNamespaceName();
49
            if (preg_match("/Silverback\\\\ApiComponentsBundle\\\\(?!Test)[\w]+/", $namespace)) {
50
                $routePrefixParts[] = '_';
51
            }
52
        }
53
54
        if (!\count($routePrefixParts)) {
55
            return $resourceMetadata;
56
        }
57
58
        return $this->prefixRoute($resourceClass, $routePrefixParts, $resourceMetadata);
59
    }
60
61
    private function prefixRoute(string $resourceClass, array $routePrefixParts, ResourceMetadataCollection $resourceMetadata): ResourceMetadataCollection
62
    {
63
        $resources = [];
64
        /** @var ApiResource $resourceMetadatum */
65
        foreach ($resourceMetadata as $i => $resourceMetadatum) {
66
            $finalRoutePrefixParts = [...$routePrefixParts];
67
            if ($currentRoutePrefix = $resourceMetadatum->getRoutePrefix()) {
68
                $finalRoutePrefixParts[] = trim($currentRoutePrefix, '/');
69
            }
70
            $newRoutePrefix = '/' . implode('/', $finalRoutePrefixParts);
71
            $resources[$i] = $resourceMetadatum->withRoutePrefix($newRoutePrefix);
72
            $newOperations = [];
73
            $oldOperations = $resourceMetadatum->getOperations();
74
            /**
75
             * @var string    $key
76
             * @var Operation $oldOperation
77
             */
78
            foreach ($oldOperations as $key => $oldOperation) {
79
                $subRoutePrefixParts = [...$routePrefixParts];
80
                if ($currentRoutePrefix = $oldOperation->getRoutePrefix()) {
81
                    $subRoutePrefixParts[] = trim($currentRoutePrefix, '/');
82
                    $subRoutePrefix = '/' . implode('/', $subRoutePrefixParts);
83
                } else {
84
                    $subRoutePrefix = $newRoutePrefix;
85
                }
86
                $newOperations[$key] = $oldOperation->withRoutePrefix($subRoutePrefix);
87
            }
88
            $resources[$i] = $resources[$i]->withOperations(new Operations($newOperations));
89
        }
90
91
        return new ResourceMetadataCollection($resourceClass, $resources);
92
    }
93
}
94