EntityNormalizer::supportsDenormalization()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 3
nc 3
nop 3
1
<?php
2
3
/*
4
 * This file is part of the vseth-semesterly-reports project.
5
 *
6
 * (c) Florian Moser <[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
namespace App\Normalizer;
13
14
use Doctrine\ORM\EntityManagerInterface;
15
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
16
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
17
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
18
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
19
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
20
21
class EntityNormalizer extends ObjectNormalizer
22
{
23
    /**
24
     * Entity manager.
25
     *
26
     * @var EntityManagerInterface
27
     */
28
    protected $em;
29
30
    /**
31
     * Entity normalizer.
32
     */
33
    public function __construct(
34
        EntityManagerInterface $em,
35
        ?ClassMetadataFactoryInterface $classMetadataFactory = null,
36
        ?NameConverterInterface $nameConverter = null,
37
        ?PropertyAccessorInterface $propertyAccessor = null,
38
        ?PropertyTypeExtractorInterface $propertyTypeExtractor = null
39
    ) {
40
        parent::__construct($classMetadataFactory, $nameConverter, $propertyAccessor, $propertyTypeExtractor);
41
        // Entity manager
42
        $this->em = $em;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function supportsDenormalization($data, $type, $format = null)
49
    {
50
        return 0 === mb_strpos($type, 'App\\Entity\\') && (is_numeric($data) || \is_string($data));
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function denormalize($data, $class, $format = null, array $context = [])
57
    {
58
        return $this->em->find($class, $data);
59
    }
60
}
61