Passed
Push — v2 ( 35ac0b...c095c8 )
by Daniel
05:46 queued 33s
created

FileInterfaceOutputClassMetadataFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 27
ccs 0
cts 13
cp 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 18 4
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Component 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\ApiComponentBundle\Metadata;
15
16
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
17
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
18
use Silverback\ApiComponentBundle\Entity\Utility\FileInterface;
19
20
/**
21
 * This is to explicitly define the output class of any resource implementing FilterInterface
22
 * This is so that the Data Transformer will be called.
23
 *
24
 * @author Daniel West <[email protected]>
25
 */
26
class FileInterfaceOutputClassMetadataFactory implements ResourceMetadataFactoryInterface
27
{
28
    private ResourceMetadataFactoryInterface $decorated;
29
30
    public function __construct(ResourceMetadataFactoryInterface $decorated)
31
    {
32
        $this->decorated = $decorated;
33
    }
34
35
    public function create(string $resourceClass): ResourceMetadata
36
    {
37
        $resourceMetadata = $this->decorated->create($resourceClass);
38
        if (!is_subclass_of($resourceClass, FileInterface::class)) {
39
            return $resourceMetadata;
40
        }
41
42
        if (!$currentOutputClass = $resourceMetadata->getAttribute('output')) {
0 ignored issues
show
Unused Code introduced by
The assignment to $currentOutputClass is dead and can be removed.
Loading history...
43
            $attributes = $resourceMetadata->getAttributes() ?: [];
44
            $resourceMetadata = $resourceMetadata->withAttributes(array_merge($attributes, [
45
                'output' => [
46
                    'class' => $resourceClass,
47
                    'name' => (new \ReflectionClass($resourceClass))->getShortName(),
48
                ],
49
            ]));
50
        }
51
52
        return $resourceMetadata;
53
    }
54
}
55