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

validateQuestion(QuestionModelInterface)   D

Complexity

Conditions 12

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 40
rs 4.8
cc 12

How to fix   Complexity   

Complexity

Complex classes like easytests.api.v1.validators.QuiestionsValidator.validateQuestion(QuestionModelInterface) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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
46
            case 3: {
47
                break;
48
            }
49
            /**
50
             * TODO
51
             * Add check for Numeric And Text Answers
52
             */
53
            default: break;
54
        }
55
    }
56
}
57