Passed
Push — feature/uploadable ( 679758...63d03b )
by Daniel
05:40
created

RoutingPrefixResourceMetadataFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 47
ccs 24
cts 25
cp 0.96
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A prefixRoute() 0 11 3
A __construct() 0 3 1
A create() 0 25 5
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\AbstractComponent;
19
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData;
20
21
/**
22
 * @author Daniel West <[email protected]>
23
 */
24
class RoutingPrefixResourceMetadataFactory implements ResourceMetadataFactoryInterface
25
{
26
    private ResourceMetadataFactoryInterface $decorated;
27
28 4
    public function __construct(ResourceMetadataFactoryInterface $decorated)
29
    {
30 4
        $this->decorated = $decorated;
31 4
    }
32
33 4
    public function create(string $resourceClass): ResourceMetadata
34
    {
35 4
        $resourceMetadata = $this->decorated->create($resourceClass);
36
37 4
        $routePrefixParts = [];
38
39 4
        if (is_subclass_of($resourceClass, AbstractComponent::class)) {
40 1
            $routePrefixParts[] = 'component';
41 3
        } elseif (is_subclass_of($resourceClass, AbstractPageData::class)) {
42 1
            $routePrefixParts[] = 'page_data';
43
        } else {
44 2
            $reflection = new \ReflectionClass($resourceClass);
45 2
            $namespace = $reflection->getNamespaceName();
46 2
            $acbNamespace = 'Silverback\ApiComponentsBundle\Entity\\';
47
48 2
            if (0 === strpos($namespace, $acbNamespace)) {
49 1
                $routePrefixParts[] = '_';
50
            }
51
        }
52
53 4
        if (!\count($routePrefixParts)) {
54 1
            return $resourceMetadata;
55
        }
56
57 3
        return $this->prefixRoute($routePrefixParts, $resourceMetadata);
58
    }
59
60 3
    private function prefixRoute(array $routePrefixParts, ResourceMetadata $resourceMetadata): ResourceMetadata
61
    {
62 3
        if ($currentRoutePrefix = $resourceMetadata->getAttribute('route_prefix')) {
63
            $routePrefixParts[] = trim($currentRoutePrefix, '/');
64
        }
65 3
        $newRoutePrefix = '/' . implode('/', $routePrefixParts);
66
67 3
        $attributes = $resourceMetadata->getAttributes() ?: [];
68
69 3
        return $resourceMetadata->withAttributes(array_merge($attributes, [
70 3
            'route_prefix' => $newRoutePrefix,
71
        ]));
72
    }
73
}
74