Passed
Pull Request — dev (#396)
by
unknown
04:24
created

easytests.api.v1.validators.QuiestionsValidator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 12
c 1
b 1
f 0
dl 0
loc 43
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
D validateQuestion(QuestionModelInterface) 0 39 12
validateQuestion 0 39 ?
1
package easytests.api.v1.validators;
2
3
import easytests.api.v1.exceptions.BadRequestException;
4
import easytests.core.models.AnswerModelInterface;
5
import easytests.core.models.QuestionModelInterface;
6
7
/**
8
 * @author SakhPrace
9
 */
10
public class QuiestionsValidator {
11
12
    private AnswersValidator answersValidator = new AnswersValidator();
13
14
    public void validateQuestion(QuestionModelInterface questionModel) throws Exception {
15
        int rightAnswersCount = 0;
16
17
        for (AnswerModelInterface answerModel: questionModel.getAnswers()) {
18
            this.answersValidator.validateAnswer(answerModel);
19
            if (answerModel.getRight()) {
20
                ++rightAnswersCount;
21
            }
22
        }
23
        switch (questionModel.getQuestionType().getId()) {
24
            case 1:
25
                if (rightAnswersCount == 0) {
26
                    throw new BadRequestException("Right Answer must be exist");
27
                }
28
                if (rightAnswersCount > 1) {
29
                    throw new BadRequestException("This Question can have only one right Answer");
30
                }
31
                break;
32
33
            case 2:
34
                if (rightAnswersCount == 0) {
35
                    throw new BadRequestException("Right Answers must be exist");
36
                }
37
                if (rightAnswersCount == 1) {
38
                    throw new BadRequestException("This Question can't have only one right Answer");
39
                }
40
                if (rightAnswersCount == questionModel.getAnswers().size()) {
41
                    throw new BadRequestException("This Question can't have all right Answers");
42
                }
43
                break;
44
45
            case 3:
46
                break;
47
48
            /**
49
             * TODO
50
             * Add check for Numeric And Text Answers
51
             */
52
            default: break;
53
        }
54
    }
55
}
56