Passed
Push — feature/uploadable ( 7590ec...83c8f5 )
by Daniel
06:44 queued 48s
created

RoutingPrefixResourceMetadataFactory::create()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 8
nop 1
dl 0
loc 25
ccs 15
cts 15
cp 1
crap 5
rs 9.4555
c 0
b 0
f 0
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 5
    public function __construct(ResourceMetadataFactoryInterface $decorated)
29
    {
30 5
        $this->decorated = $decorated;
31 5
    }
32
33 5
    public function create(string $resourceClass): ResourceMetadata
34
    {
35 5
        $resourceMetadata = $this->decorated->create($resourceClass);
36
37 5
        $routePrefixParts = [];
38
39 5
        if (is_subclass_of($resourceClass, AbstractComponent::class)) {
40 2
            $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 5
        if (!\count($routePrefixParts)) {
54 1
            return $resourceMetadata;
55
        }
56
57 4
        return $this->prefixRoute($routePrefixParts, $resourceMetadata);
58
    }
59
60 4
    private function prefixRoute(array $routePrefixParts, ResourceMetadata $resourceMetadata): ResourceMetadata
61
    {
62 4
        if ($currentRoutePrefix = $resourceMetadata->getAttribute('route_prefix')) {
63 1
            $routePrefixParts[] = trim($currentRoutePrefix, '/');
64
        }
65 4
        $newRoutePrefix = '/' . implode('/', $routePrefixParts);
66
67 4
        $attributes = $resourceMetadata->getAttributes() ?: [];
68
69 4
        return $resourceMetadata->withAttributes(array_merge($attributes, [
70 4
            'route_prefix' => $newRoutePrefix,
71
        ]));
72
    }
73
}
74