Completed
Pull Request — master (#17)
by Luc
05:27
created

QuestionEntity::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 8
dl 0
loc 22
rs 9.2
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php declare(strict_types=1);
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 Ramsey\Uuid\Uuid;
9
use VSV\GVQ_API\Common\Repositories\Entities\Entity;
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\Common\ValueObjects\Language;
14
use VSV\GVQ_API\Common\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="image_file_name", type="string", length=255, nullable=false)
56
     */
57
    private $imageFileName;
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 $imageFileName
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 $imageFileName,
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->imageFileName = $imageFileName;
100
        $this->answerEntities = $answerEntities;
101
        $this->feedback = $feedback;
102
103
        foreach ($answerEntities as $answerEntity) {
104
            $answerEntity->setQuestionEntity($this);
105
        }
106
    }
107
108
    /**
109
     * @param Question $question
110
     * @return QuestionEntity
111
     */
112
    public static function fromQuestion(Question $question): QuestionEntity
113
    {
114
        /** @var AnswerEntity[] $answerEntities */
115
        $answerEntities = array_map(
116
            function (Answer $answer) {
117
                return AnswerEntity::fromAnswer($answer);
118
            },
119
            $question->getAnswers()->toArray()
120
        );
121
122
        $questionEntity = new QuestionEntity(
123
            $question->getId()->toString(),
124
            $question->getLanguage()->toNative(),
125
            $question->getYear()->toNative(),
126
            CategoryEntity::fromCategory($question->getCategory()),
127
            $question->getText()->toNative(),
128
            $question->getImageFileName()->toNative(),
129
            new ArrayCollection($answerEntities),
130
            $question->getFeedback()->toNative()
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
        return new Question(
151
            Uuid::fromString($this->getId()),
152
            new Language($this->getLanguage()),
153
            new Year($this->getYear()),
154
            $this->getCategoryEntity()->toCategory(),
155
            new NotEmptyString($this->getText()),
156
            new NotEmptyString($this->getImageFileName()),
157
            $answers,
158
            new NotEmptyString($this->getFeedback())
159
        );
160
    }
161
162
    /**
163
     * @return string
164
     */
165
    public function getLanguage(): string
166
    {
167
        return $this->language;
168
    }
169
170
    /**
171
     * @return int
172
     */
173
    public function getYear(): int
174
    {
175
        return $this->year;
176
    }
177
178
    /**
179
     * @return CategoryEntity
180
     */
181
    public function getCategoryEntity(): CategoryEntity
182
    {
183
        return $this->categoryEntity;
184
    }
185
186
    /**
187
     * @param CategoryEntity $categoryEntity
188
     */
189
    public function setCategoryEntity(CategoryEntity $categoryEntity): void
190
    {
191
        $this->categoryEntity = $categoryEntity;
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    public function getText(): string
198
    {
199
        return $this->text;
200
    }
201
202
    /**
203
     * @return string
204
     */
205
    public function getImageFileName(): string
206
    {
207
        return $this->imageFileName;
208
    }
209
210
    /**
211
     * @return Collection
212
     */
213
    public function getAnswerEntities(): Collection
214
    {
215
        return $this->answerEntities;
216
    }
217
218
    /**
219
     * @return string
220
     */
221
    public function getFeedback(): string
222
    {
223
        return $this->feedback;
224
    }
225
}
226