Completed
Push — master ( 293319...13cdbf )
by Mikołaj
01:32
created

AnswersModel   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 37
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getAnswersByQuestionsById() 0 25 5
A getAnswersByOneQuestionId() 0 8 1
1
<?php
2
3
namespace Egzaminer\Model;
4
5
use PDO;
6
7
class AnswersModel extends AbstractModel
8
{
9
    public function getAnswersByQuestionsById(array $questions): array
10
    {
11
        if (empty($questions)) {
12
            return [];
13
        }
14
15
        $where = '';
16
        foreach ($questions as $key => $value) {
17
            $where .= ' OR question_id = :question_id_'.md5($value['id']);
18
        }
19
20
        $stmt = $this->db->prepare('SELECT * FROM answers WHERE '.trim($where, ' OR '));
21
        foreach ($questions as $key => $value) {
22
            $stmt->bindValue(':question_id_'.md5($value['id']), $value['id'], PDO::PARAM_INT);
23
        }
24
25
        $stmt->execute();
26
27
        $array = [];
28
        foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $key => $value) {
29
            $array[$value['question_id']][] = $value;
30
        }
31
32
        return $array;
33
    }
34
35
    public function getAnswersByOneQuestionId(int $questionID): array
36
    {
37
        $stmt = $this->db->prepare('SELECT * FROM answers WHERE question_id = :qid');
38
        $stmt->bindValue(':qid', $questionID, PDO::PARAM_INT);
39
        $stmt->execute();
40
41
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
42
    }
43
}
44