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

QuestionDenormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\Question\Serializers;
4
5
use League\Uri\Uri;
6
use Ramsey\Uuid\Uuid;
7
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
8
use VSV\GVQ_API\Question\Models\Answer;
9
use VSV\GVQ_API\Question\Models\Answers;
10
use VSV\GVQ_API\Question\Models\Category;
11
use VSV\GVQ_API\Question\Models\Question;
12
use VSV\GVQ_API\Question\ValueObjects\Language;
13
use VSV\GVQ_API\Question\ValueObjects\NotEmptyString;
14
use VSV\GVQ_API\Question\ValueObjects\Year;
15
16
class QuestionDenormalizer implements DenormalizerInterface
17
{
18
    /**
19
     * @var CategoryDenormalizer
20
     */
21
    private $categoryDenormalizer;
22
23
    /**
24
     * @var AnswerDenormalizer
25
     */
26
    private $answerDenormalizer;
27
28
    /**
29
     * @param CategoryDenormalizer $categoryDenormalizer
30
     * @param AnswerDenormalizer $answerDenormalizer
31
     */
32
    public function __construct(
33
        CategoryDenormalizer $categoryDenormalizer,
34
        AnswerDenormalizer $answerDenormalizer
35
    ) {
36
        $this->categoryDenormalizer = $categoryDenormalizer;
37
        $this->answerDenormalizer = $answerDenormalizer;
38
    }
39
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function denormalize($data, $class, $format = null, array $context = []): Question
45
    {
46
        $category = $this->categoryDenormalizer->denormalize(
47
            $data['category'],
48
            Category::class,
49
            $format
50
        );
51
52
        $answers = array_map(
53
            function (array $answer) use ($format) {
54
                return $this->answerDenormalizer->denormalize(
55
                    $answer,
56
                    Answer::class,
57
                    $format
58
                );
59
            },
60
            $data['answers']
61
        );
62
63
        return new Question(
64
            Uuid::fromString($data['id']),
65
            new Language($data['language']),
66
            new Year($data['year']),
67
            $category,
68
            new NotEmptyString($data['questionText']),
69
            Uri::createFromString($data['pictureUri']),
70
            new Answers(...$answers),
71
            new NotEmptyString($data['feedback'])
72
        );
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function supportsDenormalization($data, $type, $format = null): bool
79
    {
80
        return ($type === Question::class) && ($format === 'json');
81
    }
82
}
83