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

FileInterfaceOutputClassMetadataFactory::create()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 18
ccs 0
cts 10
cp 0
crap 20
rs 9.9332
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