Completed
Push — master ( a1da24...44a686 )
by Antoine
18s queued 11s
created

ObjectNormalizer::supportsNormalization()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 3
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\GraphQl\Serializer;
15
16
use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
17
use ApiPlatform\Core\Api\IriConverterInterface;
18
use ApiPlatform\Core\Util\ClassInfoTrait;
19
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
20
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
21
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
22
23
/**
24
 * Decorates the output with GraphQL metadata when appropriate, but otherwise just
25
 * passes through to the decorated normalizer.
26
 */
27
final class ObjectNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
28
{
29
    use ClassInfoTrait;
30
31
    public const FORMAT = 'graphql';
32
    public const ITEM_RESOURCE_CLASS_KEY = '#itemResourceClass';
33
    public const ITEM_IDENTIFIERS_KEY = '#itemIdentifiers';
34
35
    private $decorated;
36
    private $iriConverter;
37
    private $identifiersExtractor;
38
39
    public function __construct(NormalizerInterface $decorated, IriConverterInterface $iriConverter, IdentifiersExtractorInterface $identifiersExtractor)
40
    {
41
        $this->decorated = $decorated;
42
        $this->iriConverter = $iriConverter;
43
        $this->identifiersExtractor = $identifiersExtractor;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function supportsNormalization($data, $format = null, array $context = []): bool
50
    {
51
        return self::FORMAT === $format && $this->decorated->supportsNormalization($data, $format, $context);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Component\Serial...supportsNormalization() has too many arguments starting with $context. ( Ignorable by Annotation )

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

51
        return self::FORMAT === $format && $this->decorated->/** @scrutinizer ignore-call */ supportsNormalization($data, $format, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function hasCacheableSupportsMethod(): bool
58
    {
59
        return $this->decorated->hasCacheableSupportsMethod();
0 ignored issues
show
Bug introduced by
The method hasCacheableSupportsMethod() does not exist on Symfony\Component\Serial...zer\NormalizerInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Serial...er\SerializerNormalizer or ApiPlatform\Core\Tests\F...DocumentationNormalizer or Symfony\Component\Serial...rmalizer\TestNormalizer or Symfony\Component\Serial...sonSerializerNormalizer or Symfony\Component\Serial...wareNormalizerInterface or Symfony\Component\Serial...ectSerializerNormalizer or Symfony\Component\Serializer\Serializer. Are you sure you never get one of those? ( Ignorable by Annotation )

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

59
        return $this->decorated->/** @scrutinizer ignore-call */ hasCacheableSupportsMethod();
Loading history...
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     *
65
     * @throws UnexpectedValueException
66
     */
67
    public function normalize($object, $format = null, array $context = [])
68
    {
69
        if (isset($context['api_resource'])) {
70
            $originalResource = $context['api_resource'];
71
            unset($context['api_resource']);
72
        }
73
74
        $data = $this->decorated->normalize($object, $format, $context);
75
        if (!\is_array($data)) {
76
            throw new UnexpectedValueException('Expected data to be an array');
77
        }
78
79
        // when using an output class, get the IRI from the resource
80
        if (isset($originalResource) && isset($data['id'])) {
81
            $data['_id'] = $data['id'];
82
            $data['id'] = $this->iriConverter->getIriFromItem($originalResource);
83
        }
84
85
        $data[self::ITEM_RESOURCE_CLASS_KEY] = $this->getObjectClass($object);
86
        $data[self::ITEM_IDENTIFIERS_KEY] = $this->identifiersExtractor->getIdentifiersFromItem($object);
87
88
        return $data;
89
    }
90
}
91