Completed
Push — dev ( a70858...5b49da )
by nonanerz
06:08 queued 06:04
created

QuestionNormalizer::supportsNormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 4
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace AppBundle\Serializer;
4
5
use AppBundle\Entity\Survey\SurveyQuestion;
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 View Code Duplication
class QuestionNormalizer extends ObjectNormalizer
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 SurveyQuestion;
35
    }
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function normalize($object, $format = null, array $context = [])
40
    {
41
        if (!$this->serializer instanceof NormalizerInterface) {
42
            throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer');
43
        }
44
        /** @var SurveyQuestion $question */
45
        $question = &$object;
46
47
        return $this->serializer->normalize(new \ArrayObject([
48
            'id' => $question->getId(),
49
            'title' => $question->getTitle(),
50
            'orderNumber' => $question->getOrderNumber(),
51
            'variants' => $question->getVariants(),
52
        ]), $format, $context);
53
    }
54
}
55