EntityNormalizer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 43
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsDenormalization() 0 3 3
A __construct() 0 12 1
A denormalize() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ImportBundle\Serializer\Symfony;
6
7
use Darkilliant\ImportBundle\Resolver\EntityResolver;
8
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
9
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
10
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
11
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
12
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
13
14
/**
15
 * @internal
16
 * Entity normalizer
17
 */
18
class EntityNormalizer extends ObjectNormalizer
19
{
20
    /** @var array */
21
    protected $config;
22
23
    /** @var EntityResolver */
24
    private $resolver;
25
26 8
    public function __construct(
27
        array $config,
28
        EntityResolver $resolver,
29
        ClassMetadataFactoryInterface $classMetadataFactory = null,
30
        NameConverterInterface $nameConverter = null,
31
        PropertyAccessorInterface $propertyAccessor = null,
32
        PropertyTypeExtractorInterface $propertyTypeExtractor = null
33
    ) {
34 8
        parent::__construct($classMetadataFactory, $nameConverter, $propertyAccessor, $propertyTypeExtractor);
35
        // Entity manager
36 8
        $this->resolver = $resolver;
37 8
        $this->config = $config;
38 8
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 4
    public function supportsDenormalization($data, $type, $format = null)
44
    {
45 4
        return is_array($data) && !isset($data['_resolved']) && isset($this->config[$type]);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 4
    public function denormalize($data, $class, $format = null, array $context = [])
52
    {
53 4
        $entity = $this->resolver->resolve($class, $data, $context['entity_resolver'][$class] ?? null);
54
55 4
        $entity = $entity ?? new $class();
56
57 4
        $context['object_to_populate'] = $entity;
58 4
        $data['_resolved'] = true;
59
60 4
        return $this->serializer->denormalize($data, $class, $format, $context);
0 ignored issues
show
Bug introduced by
The method denormalize() does not exist on Symfony\Component\Serializer\SerializerInterface. It seems like you code against a sub-type of Symfony\Component\Serializer\SerializerInterface such as Symfony\Component\Serializer\Serializer or Symfony\Component\Serial...rializerCollectionDummy. ( Ignorable by Annotation )

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

60
        return $this->serializer->/** @scrutinizer ignore-call */ denormalize($data, $class, $format, $context);
Loading history...
61
    }
62
}
63