Passed
Push — main ( b2d943...872826 )
by Daniel
05:35
created

d()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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\Serializer\Normalizer;
15
16
use Silverback\ApiComponentsBundle\Utility\ApiResourceRouteFinder;
17
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
18
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait;
19
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
20
21
/**
22
 * @author Daniel West <[email protected]>
23
 */
24
class AbstractResourceNormalizer implements DenormalizerInterface, DenormalizerAwareInterface
25
{
26
    use DenormalizerAwareTrait;
27
28
    private const ALREADY_CALLED = 'ABSTRACT_COMPONENT_NORMALIZER_ALREADY_CALLED';
29
30
    private ApiResourceRouteFinder $routeFinder;
31
32
    public function __construct(ApiResourceRouteFinder $routeFinder)
33
    {
34
        $this->routeFinder = $routeFinder;
35
    }
36
37
    public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
38
    {
39
        try {
40
            return !isset($context[self::ALREADY_CALLED]) && (new \ReflectionClass($type))->isAbstract();
41
        } catch (\ReflectionException $exception) {
42
            return false;
43
        }
44
    }
45
46
    public function denormalize($data, $type, $format = null, array $context = []): mixed
47
    {
48
        $context[self::ALREADY_CALLED] = true;
49
50
        if (\is_string($data)) {
51
            $iri = $data;
52
        } else {
53
            $iri = $data['@id'] ?? null;
54
        }
55
        if ($iri) {
56
            $routeParameters = $this->routeFinder->findByIri($iri);
57
            $type = $routeParameters['_api_resource_class'];
58
        }
59
60
        return $this->denormalizer->denormalize($data, $type, $format, $context);
61
    }
62
63
    public function getSupportedTypes(?string $format): array
0 ignored issues
show
Unused Code introduced by
The parameter $format is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

63
    public function getSupportedTypes(/** @scrutinizer ignore-unused */ ?string $format): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
        return ['object' => false];
66
    }
67
}
68