Passed
Push — master ( ce7fff...9920f1 )
by Alan
04:04
created

src/GraphQl/Serializer/ObjectNormalizer.php (1 issue)

Labels
Severity
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);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function hasCacheableSupportsMethod(): bool
58
    {
59
        return $this->decorated instanceof CacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
0 ignored issues
show
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 ApiPlatform\Core\GraphQl...HttpExceptionNormalizer or Symfony\Component\Serial...er\SerializerNormalizer or ApiPlatform\Core\GraphQl...ception\ErrorNormalizer or ApiPlatform\Core\Tests\F...DocumentationNormalizer or Symfony\Component\Serial...rmalizer\TestNormalizer or Symfony\Component\Serial...sonSerializerNormalizer or ApiPlatform\Core\GraphQl...tionExceptionNormalizer or Symfony\Component\Serial...wareNormalizerInterface or Symfony\Component\Serial...ectSerializerNormalizer or ApiPlatform\Core\GraphQl...timeExceptionNormalizer 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 instanceof CacheableSupportsMethodInterface && $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
        if (!isset($originalResource)) {
80
            return $data;
81
        }
82
83
        if (isset($data['id'])) {
84
            $data['_id'] = $data['id'];
85
            $data['id'] = $this->iriConverter->getIriFromItem($originalResource);
86
        }
87
88
        $data[self::ITEM_RESOURCE_CLASS_KEY] = $this->getObjectClass($originalResource);
89
        $data[self::ITEM_IDENTIFIERS_KEY] = $this->identifiersExtractor->getIdentifiersFromItem($originalResource);
90
91
        return $data;
92
    }
93
}
94