Passed
Push — feature/VSVGVQ-7 ( 28bb1c...21bc21 )
by Luc
03:27
created

QuestionNormalizer::normalize()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 3
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\Question\Serializers;
4
5
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
6
use VSV\GVQ_API\Question\Models\Answer;
7
use VSV\GVQ_API\Question\Models\Question;
8
9
class QuestionNormalizer implements NormalizerInterface
10
{
11
    /**
12
     * @var CategoryNormalizer
13
     */
14
    private $categoryNormalizer;
15
16
    /**
17
     * @var AnswerNormalizer
18
     */
19
    private $answerNormalizer;
20
21
    /**
22
     * @param CategoryNormalizer $categoryNormalizer
23
     * @param AnswerNormalizer $answerNormalizer
24
     */
25
    public function __construct(
26
        CategoryNormalizer $categoryNormalizer,
27
        AnswerNormalizer $answerNormalizer
28
    ) {
29
        $this->categoryNormalizer = $categoryNormalizer;
30
        $this->answerNormalizer = $answerNormalizer;
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function normalize($question, $format = null, array $context = []): array
37
    {
38
        $category = $this->categoryNormalizer->normalize(
39
            $question->getCategory(),
40
            $format
41
        );
42
43
        $answers = array_map(
44
            function (Answer $answer) use ($format) {
45
                return $this->answerNormalizer->normalize(
46
                    $answer,
47
                    $format
48
                );
49
            },
50
            $question->getAnswers()->toArray()
51
        );
52
53
        /** @var Question $question */
54
        return [
55
            'id' => $question->getId()->toString(),
56
            'language' => $question->getLanguage()->toNative(),
57
            'year' => $question->getYear()->toNative(),
58
            'category' => $category,
59
            'questionText' => $question->getQuestionText()->toNative(),
60
            'pictureUri' => $question->getPictureUri()->__toString(),
61
            'answers' => $answers,
62
            'feedback' => $question->getFeedback()->toNative(),
63
        ];
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function supportsNormalization($data, $format = null): bool
70
    {
71
        return ($data instanceof Question) && ($format === 'json');
72
    }
73
}
74