Completed
Push — master ( ac011d...56c397 )
by Mikołaj
03:19
created

QuestionAddModel   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 22.09 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 1
dl 19
loc 86
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 11 2
A addQuestion() 10 10 1
A addCorrectAnswerToQuestion() 9 9 1
B addAnswers() 0 26 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Egzaminer\Model;
4
5
use PDO;
6
7
class QuestionAddModel extends AbstractModel
8
{
9
    public function add($examID, $post)
10
    {
11
        if (!isset($post['question']['correct'])) {
12
            $post['question']['correct'] = 0;
13
        }
14
        $qid = $this->addQuestion($examID, $post['question']);
15
        $cid = $this->addAnswers($examID, $qid, $post['question']['correct'], $post['answers']);
16
        $this->addCorrectAnswerToQuestion($qid, $cid);
17
18
        return $qid;
19
    }
20
21
    /**
22
     * Add question for exam.
23
     *
24
     * @param int   $examID
25
     * @param array $question
26
     *
27
     * @return book
28
     */
29 View Code Duplication
    private function addQuestion($examID, $question)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        $stmt = $this->db->prepare('INSERT INTO questions (exam_id, content)
32
            VALUES (:exam_id, :content)');
33
        $stmt->bindValue(':exam_id', $examID, PDO::PARAM_INT);
34
        $stmt->bindValue(':content', trim($question['content']), PDO::PARAM_STR);
35
        $stmt->execute();
36
37
        return $this->db->lastInsertId();
38
    }
39
40
    /**
41
     * Add correct answer to question.
42
     *
43
     * @param int $questionID Question id
44
     * @param int $correct    Correct anwer id
45
     */
46 View Code Duplication
    public function addCorrectAnswerToQuestion($questionID, $correct)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $stmt = $this->db->prepare('UPDATE questions SET correct = :correct
49
            WHERE id = :id');
50
        $stmt->bindValue(':correct', $correct, PDO::PARAM_INT);
51
        $stmt->bindValue(':id', $questionID, PDO::PARAM_INT);
52
53
        return $stmt->execute();
54
    }
55
56
    /**
57
     * Add answers for exam.
58
     *
59
     * @param int   $examID
60
     * @param int   $questionID
61
     * @param int   $correct
62
     * @param array $answers
63
     *
64
     * @return bool
65
     */
66
    private function addAnswers($examID, $questionID, $correct, $answers)
67
    {
68
        $stmt = $this->db->prepare('INSERT INTO answers (exam_id, question_id, content)
69
            VALUES (:exam_id, :question_id, :content)
70
        ');
71
        $this->db->beginTransaction();
72
73
        foreach ($answers as $key => $value) {
74
            $stmt->bindValue(':exam_id', $examID, PDO::PARAM_INT);
75
            $stmt->bindValue(':question_id', $questionID, PDO::PARAM_INT);
76
            $stmt->bindValue(':content', trim($value), PDO::PARAM_STR);
77
            $stmt->execute();
78
79
            if ($correct == $key) {
80
                $correctId = $this->db->lastInsertId();
81
            }
82
        }
83
84
        if (!isset($correctId)) {
85
            $correctId = 0;
86
        }
87
88
        $this->db->commit();
89
90
        return $correctId;
91
    }
92
}
93