Completed
Pull Request — master (#17)
by Luc
07:11 queued 03:45
created

QuestionFormDTO::toExistingQuestion()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 23
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\Question\Forms;
4
5
use Ramsey\Uuid\UuidFactoryInterface;
6
use Symfony\Component\HttpFoundation\File\UploadedFile;
7
use VSV\GVQ_API\Common\ValueObjects\Language;
8
use VSV\GVQ_API\Common\ValueObjects\NotEmptyString;
9
use VSV\GVQ_API\Question\Models\Answer;
10
use VSV\GVQ_API\Question\Models\Answers;
11
use VSV\GVQ_API\Question\Models\Category;
12
use VSV\GVQ_API\Question\Models\Question;
13
use VSV\GVQ_API\Question\ValueObjects\Year;
14
15
class QuestionFormDTO
16
{
17
    /**
18
     * @var Language
19
     */
20
    public $language;
21
22
    /**
23
     * @var int
24
     */
25
    public $year;
26
27
    /**
28
     * @var Category
29
     */
30
    public $category;
31
32
    /**
33
     * @var UploadedFile
34
     */
35
    public $image;
36
37
    /**
38
     * @var string
39
     */
40
    public $text;
41
42
    /**
43
     * @var string
44
     */
45
    public $answer1;
46
47
    /**
48
     * @var string
49
     */
50
    public $answer2;
51
52
    /**
53
     * @var string
54
     */
55
    public $answer3;
56
57
    /**
58
     * @var int
59
     */
60
    public $correctAnswer;
61
62
    /**
63
     * @var string
64
     */
65
    public $feedback;
66
67
    /**
68
     * QuestionForm constructor.
69
     */
70
    public function __construct()
71
    {
72
        $this->year = 2018;
73
    }
74
75
    /**
76
     * @param UuidFactoryInterface $uuidFactory
77
     * @param NotEmptyString $fileName
78
     * @return Question
79
     */
80
    public function toNewQuestion(
81
        UuidFactoryInterface $uuidFactory,
82
        NotEmptyString $fileName
83
    ): Question {
84
        // TODO: Take into account the correct number of answers, can be 2 or 3.
85
        $answers = new Answers(
86
            new Answer(
87
                $uuidFactory->uuid4(),
88
                new NotEmptyString($this->answer1),
89
                $this->correctAnswer === 1 ? true : false
90
            ),
91
            new Answer(
92
                $uuidFactory->uuid4(),
93
                new NotEmptyString($this->answer2),
94
                $this->correctAnswer === 2 ? true : false
95
            ),
96
            new Answer(
97
                $uuidFactory->uuid4(),
98
                new NotEmptyString($this->answer3),
99
                $this->correctAnswer === 3 ? true : false
100
            )
101
        );
102
103
        return new Question(
104
            $uuidFactory->uuid4(),
105
            $this->language,
106
            new Year($this->year),
107
            $this->category,
108
            new NotEmptyString($this->text),
109
            $fileName,
110
            $answers,
111
            new NotEmptyString($this->feedback)
112
        );
113
    }
114
115
    /**
116
     * @param Question $existingQuestion
117
     * @return Question
118
     */
119
    public function toExistingQuestion(Question $existingQuestion): Question
120
    {
121
        // TODO: Take into account the correct number of answers, can be 2 or 3.
122
        $existingAnswers = $existingQuestion->getAnswers()->toArray();
123
124
        $answers = new Answers(
125
            new Answer(
126
                $existingAnswers[0]->getId(),
127
                new NotEmptyString($this->answer1),
128
                $this->correctAnswer === 1 ? true : false
129
            ),
130
            new Answer(
131
                $existingAnswers[1]->getId(),
132
                new NotEmptyString($this->answer2),
133
                $this->correctAnswer === 2 ? true : false
134
            ),
135
            new Answer(
136
                $existingAnswers[2]->getId(),
137
                new NotEmptyString($this->answer3),
138
                $this->correctAnswer === 3 ? true : false
139
            )
140
        );
141
142
        return new Question(
143
            $existingQuestion->getId(),
144
            $this->language,
145
            new Year($this->year),
146
            $this->category,
147
            new NotEmptyString($this->text),
148
            $existingQuestion->getImageFileName(),
149
            $answers,
150
            new NotEmptyString($this->feedback)
151
        );
152
    }
153
154
    /**
155
     * @param Question $question
156
     */
157
    public function fromQuestion(Question $question): void
158
    {
159
        $this->language = $question->getLanguage();
160
        $this->year = $question->getYear()->toNative();
161
        $this->category = $question->getCategory();
162
163
        $this->text = $question->getText()->toNative();
164
165
        $answers = $question->getAnswers()->toArray();
166
        $this->answer1 = $answers[0]->getText()->toNative();
167
        $this->answer2 = $answers[1]->getText()->toNative();
168
        $this->answer3 = $answers[2]->getText()->toNative();
169
        $this->correctAnswer = 1;
170
171
        $this->feedback = $question->getFeedback()->toNative();
172
    }
173
}
174