Completed
Push — feature/VSVGVQ-7-Entities ( cc0453 )
by Luc
02:51
created

QuestionEntity   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 257
rs 10
c 0
b 0
f 0
wmc 18

17 Methods

Rating   Name   Duplication   Size   Complexity  
A setText() 0 3 1
B fromQuestion() 0 26 2
A getText() 0 3 1
A __construct() 0 19 1
A setYear() 0 3 1
A setCategoryEntity() 0 3 1
B toQuestion() 0 26 1
A getCategoryEntity() 0 3 1
A getLanguage() 0 3 1
A setLanguage() 0 3 1
A getAnswerEntities() 0 3 1
A setAnswerEntities() 0 3 1
A setPictureUri() 0 3 1
A getFeedback() 0 3 1
A getYear() 0 3 1
A getPictureUri() 0 3 1
A setFeedback() 0 3 1
1
<?php
2
3
namespace VSV\GVQ_API\Question\Repositories\Entities;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\ORM\Mapping as ORM;
8
use League\Uri\Uri;
9
use Ramsey\Uuid\Uuid;
10
use VSV\GVQ_API\Question\Models\Answer;
11
use VSV\GVQ_API\Question\Models\Answers;
12
use VSV\GVQ_API\Question\Models\Question;
13
use VSV\GVQ_API\Question\ValueObjects\Language;
14
use VSV\GVQ_API\Question\ValueObjects\NotEmptyString;
15
use VSV\GVQ_API\Question\ValueObjects\Year;
16
17
/**
18
 * @ORM\Entity()
19
 * @ORM\Table(name="question")
20
 */
21
class QuestionEntity extends Entity
22
{
23
    /**
24
     * @var string
25
     *
26
     * @ORM\Column(type="string", length=2, nullable=false)
27
     */
28
    private $language;
29
30
    /**
31
     * @var int
32
     *
33
     * @ORM\Column(type="smallint", nullable=false)
34
     */
35
    private $year;
36
37
    /**
38
     * @var CategoryEntity
39
     *
40
     * @ORM\ManyToOne(targetEntity="CategoryEntity", fetch="EAGER")
41
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=false)
42
     */
43
    private $categoryEntity;
44
45
    /**
46
     * @var string
47
     *
48
     * @ORM\Column(type="string", length=1024, nullable=false)
49
     */
50
    private $text;
51
52
    /**
53
     * @var string
54
     *
55
     * @ORM\Column(name="picture_uri", type="string", length=255, nullable=false)
56
     */
57
    private $pictureUri;
58
59
    /**
60
     * @var Collection
61
     *
62
     * @ORM\OneToMany(targetEntity="AnswerEntity", mappedBy="questionEntity", cascade={"all"})
63
     */
64
    private $answerEntities;
65
66
    /**
67
     * @var string
68
     *
69
     * @ORM\Column(type="string", length=1024, nullable=false)
70
     */
71
    private $feedback;
72
73
    /**
74
     * @param string $id
75
     * @param string $language
76
     * @param int $year
77
     * @param CategoryEntity $categoryEntity
78
     * @param string $text
79
     * @param string $pictureUri
80
     * @param Collection $answerEntities
81
     * @param string $feedback
82
     */
83
    private function __construct(
84
        string $id,
85
        string $language,
86
        int $year,
87
        CategoryEntity $categoryEntity,
88
        string $text,
89
        string $pictureUri,
90
        Collection $answerEntities,
91
        string $feedback
92
    ) {
93
        parent::__construct($id);
94
95
        $this->language = $language;
96
        $this->year = $year;
97
        $this->categoryEntity = $categoryEntity;
98
        $this->text = $text;
99
        $this->pictureUri = $pictureUri;
100
        $this->answerEntities = $answerEntities;
101
        $this->feedback = $feedback;
102
    }
103
104
    /**
105
     * @param Question $question
106
     * @return QuestionEntity
107
     */
108
    public static function fromQuestion(Question $question): QuestionEntity
109
    {
110
        /** @var AnswerEntity[] $answerEntities */
111
        $answerEntities = array_map(
112
            function (Answer $answer) {
113
                return AnswerEntity::fromAnswer($answer);
114
            },
115
            $question->getAnswers()->toArray()
116
        );
117
118
        $questionEntity = new QuestionEntity(
119
            $question->getId()->toString(),
120
            $question->getLanguage()->toNative(),
121
            $question->getYear()->toNative(),
122
            CategoryEntity::fromCategory($question->getCategory()),
123
            $question->getText()->toNative(),
124
            $question->getPictureUri()->__toString(),
125
            new ArrayCollection($answerEntities),
126
            $question->getFeedback()->toNative()
127
        );
128
129
        foreach ($answerEntities as $answerEntity) {
130
            $answerEntity->setQuestionEntity($questionEntity);
131
        }
132
133
        return $questionEntity;
134
    }
135
136
    /**
137
     * @return Question
138
     */
139
    public function toQuestion(): Question
140
    {
141
        $answers = new Answers(
142
            ...array_map(
143
                function(AnswerEntity $answerEntity) {
144
                    return $answerEntity->toAnswer();
145
                },
146
                $this->getAnswerEntities()->toArray()
147
            )
148
        );
149
150
        // Call toString method to solve side effect in Uri lib because the data
151
        // property gets created on calling this getter method.
152
        // @see https://github.com/thephpleague/uri-schemes/issues/10
153
        $pictureUri = Uri::createFromString($this->getPictureUri());
154
        $pictureUri->__toString();
155
156
        return new Question(
157
            Uuid::fromString($this->getId()),
158
            new Language($this->getLanguage()),
159
            new Year($this->getYear()),
160
            $this->getCategoryEntity()->toCategory(),
161
            new NotEmptyString($this->getText()),
162
            $pictureUri,
163
            $answers,
164
            new NotEmptyString($this->getFeedback())
165
        );
166
    }
167
168
    /**
169
     * @return string
170
     */
171
    public function getLanguage(): string
172
    {
173
        return $this->language;
174
    }
175
176
    /**
177
     * @param string $language
178
     */
179
    public function setLanguage(string $language): void
180
    {
181
        $this->language = $language;
182
    }
183
184
    /**
185
     * @return int
186
     */
187
    public function getYear(): int
188
    {
189
        return $this->year;
190
    }
191
192
    /**
193
     * @param int $year
194
     */
195
    public function setYear(int $year): void
196
    {
197
        $this->year = $year;
198
    }
199
200
    /**
201
     * @return CategoryEntity
202
     */
203
    public function getCategoryEntity(): CategoryEntity
204
    {
205
        return $this->categoryEntity;
206
    }
207
208
    /**
209
     * @param CategoryEntity $categoryEntity
210
     */
211
    public function setCategoryEntity(CategoryEntity $categoryEntity): void
212
    {
213
        $this->categoryEntity = $categoryEntity;
214
    }
215
216
    /**
217
     * @return string
218
     */
219
    public function getText(): string
220
    {
221
        return $this->text;
222
    }
223
224
    /**
225
     * @param string $text
226
     */
227
    public function setText(string $text): void
228
    {
229
        $this->text = $text;
230
    }
231
232
    /**
233
     * @return string
234
     */
235
    public function getPictureUri(): string
236
    {
237
        return $this->pictureUri;
238
    }
239
240
    /**
241
     * @param string $pictureUri
242
     */
243
    public function setPictureUri(string $pictureUri): void
244
    {
245
        $this->pictureUri = $pictureUri;
246
    }
247
248
    /**
249
     * @return Collection
250
     */
251
    public function getAnswerEntities(): Collection
252
    {
253
        return $this->answerEntities;
254
    }
255
256
    /**
257
     * @param Collection $answerEntities
258
     */
259
    public function setAnswerEntities(Collection $answerEntities): void
260
    {
261
        $this->answerEntities = $answerEntities;
262
    }
263
264
    /**
265
     * @return string
266
     */
267
    public function getFeedback(): string
268
    {
269
        return $this->feedback;
270
    }
271
272
    /**
273
     * @param string $feedback
274
     */
275
    public function setFeedback(string $feedback): void
276
    {
277
        $this->feedback = $feedback;
278
    }
279
}
280