Test Failed
Push — master ( 09495e...5db298 )
by Daniel
04:30
created

ComponentResourceMetadataFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 22 2
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 ApiPlatform\Core\Operation\PathSegmentNameGeneratorInterface;
19
use Silverback\ApiComponentsBundle\Entity\Core\ComponentInterface;
20
21
/**
22
 * This will add an endpoint for component resources to find out usage totals.
23
 *
24
 * @author Daniel West <[email protected]>
25
 */
26
class ComponentResourceMetadataFactory implements ResourceMetadataFactoryInterface
27
{
28
    private ResourceMetadataFactoryInterface $decorated;
29
    private PathSegmentNameGeneratorInterface $pathSegmentNameGenerator;
30
31
    public function __construct(ResourceMetadataFactoryInterface $decorated, PathSegmentNameGeneratorInterface $pathSegmentNameGenerator)
32
    {
33
        $this->decorated = $decorated;
34
        $this->pathSegmentNameGenerator = $pathSegmentNameGenerator;
35
    }
36
37
    public function create(string $resourceClass): ResourceMetadata
38
    {
39
        $resourceMetadata = $this->decorated->create($resourceClass);
40
        $interfaces = class_implements($resourceClass);
41
        if (!\in_array(ComponentInterface::class, $interfaces, true)) {
42
            return $resourceMetadata;
43
        }
44
45
        $resourceShortName = $resourceMetadata->getShortName();
46
        $pathSegmentName = $this->pathSegmentNameGenerator->getSegmentName($resourceShortName);
0 ignored issues
show
Bug introduced by
It seems like $resourceShortName can also be of type null; however, parameter $name of ApiPlatform\Core\Operati...rface::getSegmentName() does only seem to accept string, 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

46
        $pathSegmentName = $this->pathSegmentNameGenerator->getSegmentName(/** @scrutinizer ignore-type */ $resourceShortName);
Loading history...
47
48
        $usagePath = sprintf('/%s/{id}/usage', $pathSegmentName);
49
50
        $itemOperations = $resourceMetadata->getItemOperations() ?? [];
51
        $itemOperations['get_usage'] = [
52
            'method' => 'GET',
53
            'stateless' => null,
54
            'path' => $usagePath,
55
            'serialize' => true,
56
        ];
57
58
        return $resourceMetadata->withItemOperations($itemOperations);
59
    }
60
}
61