Completed
Pull Request — master (#1)
by Luc
06:59 queued 03:12
created

Question::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
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\Models;
4
5
use League\Uri\Interfaces\Uri;
6
use Ramsey\Uuid\UuidInterface;
7
use VSV\GVQ_API\Question\ValueObjects\Language;
8
use VSV\GVQ_API\Question\ValueObjects\NotEmptyString;
9
use VSV\GVQ_API\Question\ValueObjects\Year;
10
11
class Question
12
{
13
    /**
14
     * @var UuidInterface
15
     */
16
    private $id;
17
18
    /**
19
     * @var Language
20
     */
21
    private $language;
22
23
    /**
24
     * @var Year
25
     */
26
    private $year;
27
28
    /**
29
     * @var Category
30
     */
31
    private $category;
32
33
    /**
34
     * @var NotEmptyString
35
     */
36
    private $questionText;
37
38
    /**
39
     * @var Uri
40
     */
41
    private $pictureUri;
42
43
    /**
44
     * @var Answer[]
45
     */
46
    private $answers;
47
48
    /**
49
     * @var NotEmptyString
50
     */
51
    private $feedback;
52
53
    /**
54
     * @var \DateTimeImmutable
55
     */
56
    private $archivedOn;
57
58
    /**
59
     * @param UuidInterface $id
60
     * @param Language $language
61
     * @param Year $year
62
     * @param Category $category
63
     * @param NotEmptyString $questionText
64
     * @param Uri $pictureUri
65
     * @param Answers $answers
66
     * @param NotEmptyString $feedback
67
     */
68
    public function __construct(
69
        UuidInterface $id,
70
        Language $language,
71
        Year $year,
72
        Category $category,
73
        NotEmptyString $questionText,
74
        Uri $pictureUri,
75
        Answers $answers,
76
        NotEmptyString $feedback
77
    ) {
78
        if (count($answers) < 2 || count($answers) > 3) {
79
            throw new \InvalidArgumentException('Amount of answers must be 2 or 3.');
80
        }
81
82
        $this->id = $id;
83
        $this->language = $language;
84
        $this->year = $year;
85
        $this->category = $category;
86
        $this->questionText = $questionText;
87
        $this->pictureUri = $pictureUri;
88
        $this->answers = $answers->toArray();
89
        $this->feedback = $feedback;
90
    }
91
92
    /**
93
     * @return UuidInterface
94
     */
95
    public function getId(): UuidInterface
96
    {
97
        return $this->id;
98
    }
99
100
    /**
101
     * @return Language
102
     */
103
    public function getLanguage(): Language
104
    {
105
        return $this->language;
106
    }
107
108
    /**
109
     * @return Year
110
     */
111
    public function getYear(): Year
112
    {
113
        return $this->year;
114
    }
115
116
    /**
117
     * @return Category
118
     */
119
    public function getCategory(): Category
120
    {
121
        return $this->category;
122
    }
123
124
    /**
125
     * @return NotEmptyString
126
     */
127
    public function getQuestionText(): NotEmptyString
128
    {
129
        return $this->questionText;
130
    }
131
132
    /**
133
     * @return Uri
134
     */
135
    public function getPictureUri(): Uri
136
    {
137
        return $this->pictureUri;
138
    }
139
140
    /**
141
     * @return Answers
142
     */
143
    public function getAnswers(): Answers
144
    {
145
        return new Answers(...$this->answers);
146
    }
147
148
    /**
149
     * @return NotEmptyString
150
     */
151
    public function getFeedback(): NotEmptyString
152
    {
153
        return $this->feedback;
154
    }
155
156
    /**
157
     * @param \DateTimeImmutable $archiveOn
158
     */
159
    public function archiveOn(\DateTimeImmutable $archiveOn): void
160
    {
161
        if ($this->archivedOn !== null) {
162
            throw new \DomainException(
163
                'The question with id: "'.$this->getId()->toString().'" was already archived.'
164
            );
165
        }
166
167
        $this->archivedOn = $archiveOn;
168
    }
169
170
    /**
171
     * @return bool
172
     */
173
    public function isArchived(): bool
174
    {
175
        return $this->archivedOn !== null;
176
    }
177
178
    /**
179
     * @return \DateTimeImmutable
180
     */
181
    public function getArchivedOn(): ?\DateTimeImmutable
182
    {
183
        return $this->archivedOn;
184
    }
185
}
186