Completed
Push — master ( d2b1b9...c80d49 )
by Antoine
22s queued 13s
created

InputOutputMetadataTrait::getInputClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[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 ApiPlatform\Core\Serializer;
15
16
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
17
18
trait InputOutputMetadataTrait
19
{
20
    /**
21
     * @var \ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface
22
     */
23
    protected $resourceMetadataFactory;
24
25
    private function getInputOutputMetadata(string $class, string $inputOrOutput, array $context)
26
    {
27
        if (null === $this->resourceMetadataFactory || null !== ($context[$inputOrOutput]['class'] ?? null)) {
28
            return $context[$inputOrOutput]['class'] ?? null;
29
        }
30
31
        try {
32
            $metadata = $this->resourceMetadataFactory->create($class);
33
        } catch (ResourceClassNotFoundException $e) {
34
            return null;
35
        }
36
37
        return $metadata->getAttribute($inputOrOutput)['class'] ?? null;
38
    }
39
40
    protected function getInputClass(string $class, array $context = []): ?string
41
    {
42
        return $this->getInputOutputMetadata($class, 'input', $context);
43
    }
44
45
    protected function getOutputClass(string $class, array $context = []): ?string
46
    {
47
        return $this->getInputOutputMetadata($class, 'output', $context);
48
    }
49
}
50