Completed
Pull Request — master (#30)
by
unknown
05:03
created

SurveyAnswerNormalizer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 67.65%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 90
ccs 23
cts 34
cp 0.6765
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A supportsNormalization() 0 4 1
A normalize() 0 13 2
C denormalize() 0 28 7
A supportsDenormalization() 0 8 2
1
<?php
2
3
namespace AppBundle\Serializer;
4
5
use AppBundle\Entity\Survey\Survey;
6
use AppBundle\Entity\Survey\SurveyAnswer;
7
use AppBundle\Entity\Survey\SurveyQuestion;
8
use Doctrine\Bundle\DoctrineBundle\Registry;
9
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
10
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
11
use Symfony\Component\Serializer\Exception\LogicException;
12
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
13
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
14
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
15
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
16
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
17
18
class SurveyAnswerNormalizer extends ObjectNormalizer
19
{
20
    /**
21
     * @var Registry
22
     */
23
    protected $doctrine;
24
25
    /**
26
     * SurveyAnswerNormalizer constructor.
27
     *
28
     * @param ClassMetadataFactoryInterface|null  $classMDF
29
     * @param NameConverterInterface|null         $nameCv
30
     * @param PropertyAccessorInterface|null      $propertyAs
0 ignored issues
show
Bug introduced by
There is no parameter named $propertyAs. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
31
     * @param PropertyTypeExtractorInterface|null $propTE
32
     * @param Registry                            $doctrine
33
     */
34 4
    public function __construct($classMDF, $nameCv, $propAs, $propTE, Registry $doctrine)
35
    {
36 4
        parent::__construct($classMDF, $nameCv, $propAs, $propTE);
37 4
        $this->doctrine = $doctrine;
38 4
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 4
    public function supportsNormalization($data, $format = null)
44
    {
45 4
        return $data instanceof SurveyAnswer;
46
    }
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function normalize($object, $format = null, array $context = [])
51
    {
52
        if (!$this->serializer instanceof NormalizerInterface) {
53
            throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer');
54
        }
55
        /** @var SurveyAnswer $answer */
56
        $answer = &$object;
57
58
        return $this->serializer->normalize(new \ArrayObject([
59
            'questionId' => $answer->getQuestion()->getId(),
60
            'content' => $answer->getContent(),
61
        ]), $format, $context);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function denormalize($data, $class, $format = null, array $context = array())
68
    {
69 1
        if (!$this->serializer instanceof DenormalizerInterface) {
70
            throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer');
71
        }
72
        /** @var Survey $survey */
73 1
        $survey = $context[ObjectNormalizer::OBJECT_TO_POPULATE];
74
75 1
        if (!array_key_exists('questionId', $data) || !array_key_exists('content', $data)) {
76
            throw new LogicException('Wrong json consruction');
77
        }
78
79 1
        $newAnswer = new SurveyAnswer();
80 1
        $question = $this->doctrine->getManager()->getRepository(SurveyQuestion::class)
81 1
            ->find($data['questionId']);
82 1
        if (!in_array($question, $survey->getQuestions())) {
83
            throw new LogicException('Wrong question id');
84
        }
85 1
        if ($question->getVariants()) {
86 1
            if (!in_array($data['content'], $question->getVariants())) {
87
                throw new LogicException('Wrong variants');
88
            }
89
        }
90 1
        $newAnswer->setQuestion($question);
91 1
        $newAnswer->setContent($data['content']);
92
93 1
        return $newAnswer;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 1
    public function supportsDenormalization($data, $type, $format = null)
100
    {
101 1
        if ($type != SurveyAnswer::class) {
102 1
            return false;
103
        }
104
105 1
        return true;
106
    }
107
}
108