TypeNormalizer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 65%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 48
ccs 13
cts 20
cp 0.65
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A supportsNormalization() 0 4 1
B normalize() 0 22 4
1
<?php
2
3
namespace AppBundle\Serializer;
4
5
use AppBundle\Entity\Survey\SurveyType;
6
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
7
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
8
use Symfony\Component\Serializer\Exception\LogicException;
9
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
10
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
11
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
12
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
13
14
class TypeNormalizer extends ObjectNormalizer
15
{
16
    /**
17
     * SurveyNormalizer constructor.
18
     *
19
     * @param ClassMetadataFactoryInterface|null  $classMDF
20
     * @param NameConverterInterface|null         $nameCv
21
     * @param PropertyAccessorInterface|null      $propAs
22
     * @param PropertyTypeExtractorInterface|null $propTE
23
     */
24 5
    public function __construct($classMDF, $nameCv, $propAs, $propTE)
25
    {
26 5
        parent::__construct($classMDF, $nameCv, $propAs, $propTE);
27 5
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 5
    public function supportsNormalization($data, $format = null)
33
    {
34 5
        return $data instanceof SurveyType;
35
    }
36
    /**
37
     * {@inheritdoc}
38
     */
39 2
    public function normalize($object, $format = null, array $context = [])
40
    {
41 2
        if (!$this->serializer instanceof NormalizerInterface) {
42
            throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer');
43
        }
44
        /** @var SurveyType $type */
45 2
        $type = &$object;
46 2
        if (isset($context[ObjectNormalizer::GROUPS])) {
47 2
            if ($context[ObjectNormalizer::GROUPS][0] == 'list') {
48 2
                return $this->serializer->normalize(new \ArrayObject([
49 2
                    'name' => $type->getName(),
50 2
                ]), $format, $context);
51
            }
52
        }
53
54
        return $this->serializer->normalize(new \ArrayObject([
55
            'id' => $type->getId(),
56
            'name' => $type->getName(),
57
            'description' => $type->getDescription(),
58
            'section' => $type->getSections(),
59
        ]), $format, $context);
60
    }
61
}
62