Passed
Push — master ( 8bd912...d93388 )
by Alan
06:58 queued 02:20
created

Bridge/Elasticsearch/Serializer/ItemNormalizer.php (1 issue)

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\Bridge\Elasticsearch\Serializer;
15
16
use ApiPlatform\Core\Bridge\Elasticsearch\Api\IdentifierExtractorInterface;
17
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
18
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
19
use Symfony\Component\Serializer\Exception\LogicException;
20
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
21
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
22
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
23
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
24
25
/**
26
 * Item denormalizer for Elasticsearch.
27
 *
28
 * @experimental
29
 *
30
 * @author Baptiste Meyer <[email protected]>
31
 */
32
final class ItemNormalizer extends ObjectNormalizer
33
{
34
    public const FORMAT = 'elasticsearch';
35
36
    private $identifierExtractor;
37
38
    public function __construct(IdentifierExtractorInterface $identifierExtractor, ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = [])
39
    {
40
        parent::__construct($classMetadataFactory, $nameConverter, $propertyAccessor, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext);
41
42
        $this->identifierExtractor = $identifierExtractor;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function supportsDenormalization($data, $type, $format = null): bool
49
    {
50
        return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function denormalize($data, $class, $format = null, array $context = [])
57
    {
58
        if (\is_string($data['_id'] ?? null) && \is_array($data['_source'] ?? null)) {
59
            $data = $this->populateIdentifier($data, $class)['_source'];
60
        }
61
62
        return parent::denormalize($data, $class, $format, $context);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function supportsNormalization($data, $format = null): bool
69
    {
70
        // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format
71
        return self::FORMAT === $format;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     *
77
     * @throws LogicException
78
     */
79
    public function normalize($object, $format = null, array $context = [])
80
    {
81
        throw new LogicException(sprintf('%s is a write-only format.', self::FORMAT));
82
    }
83
84
    /**
85
     * Populates the resource identifier with the document identifier if not present in the original JSON document.
86
     */
87
    private function populateIdentifier(array $data, string $class): array
88
    {
89
        $identifier = $this->identifierExtractor->getIdentifierFromResourceClass($class);
90
        $identifier = null === $this->nameConverter ? $identifier : $this->nameConverter->normalize($identifier, $class, self::FORMAT);
0 ignored issues
show
The call to Symfony\Component\Serial...rInterface::normalize() has too many arguments starting with $class. ( Ignorable by Annotation )

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

90
        $identifier = null === $this->nameConverter ? $identifier : $this->nameConverter->/** @scrutinizer ignore-call */ normalize($identifier, $class, self::FORMAT);

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...
91
92
        if (!isset($data['_source'][$identifier])) {
93
            $data['_source'][$identifier] = $data['_id'];
94
        }
95
96
        return $data;
97
    }
98
}
99