IndividualDAO   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndividual() 0 27 2
A addIndividual() 0 14 1
A __construct() 0 2 1
A getAllIndividuals() 0 27 2
A getIndividualsStartingWith() 0 34 4
1
<?php
2
namespace AL\Common\DAO;
3
4
require_once __DIR__."/../../lib/Db.php";
5
require_once __DIR__."/../Model/Individual/Individual.php";
6
7
/**
8
 * DAO for Individual
9
 */
10
class IndividualDAO {
11
    private $mysqli;
12
13
    public function __construct($mysqli) {
14
        $this->mysqli = $mysqli;
15
    }
16
17
    /**
18
     * Get all individuals
19
     * @return \AL\Common\Model\Individual\Individual[] A list of individuals
20
     */
21
    public function getAllIndividuals() {
22
        $stmt = \AL\Db\execute_query(
23
            "IndividualDAO: getAllIndividuals",
24
            $this->mysqli,
25
            "SELECT ind_id, ind_name FROM individuals ORDER BY ind_name ASC",
26
            null,
27
            null
28
        );
29
30
        \AL\Db\bind_result(
31
            "IndividualDAO: getAllIndividuals",
32
            $stmt,
33
            $id,
34
            $name
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
35
        );
36
37
        $individuals = [];
38
        while ($stmt->fetch()) {
39
            $individuals[] = new \AL\Common\Model\Individual\Individual(
40
                $id,
41
                $name
42
            );
43
        }
44
45
        $stmt->close();
46
47
        return $individuals;
48
    }
49
50
    /**
51
     * Browse individuals by first letter
52
     * @param string $search_string can either be a single letter or "num" which represents numbers
53
     * @return \AL\Common\Model\Individual\Individual[] A list of individuals
54
     */
55
    public function getIndividualsStartingWith($search_string) {
56
        if (isset($search_string) and $search_string == "num") {
57
            $search_string = "^[0-9]";
58
        } else {
59
            $search_string = "^$search_string";
60
        }
61
62
        $stmt = \AL\Db\execute_query(
63
            "IndividualDAO: getIndividualsStartingWith",
64
            $this->mysqli,
65
            "SELECT ind_id, ind_name FROM individuals
66
            WHERE LOWER(ind_name) REGEXP ? ORDER BY ind_name ASC",
67
            "s",
68
            strtolower($search_string)
69
        );
70
71
        \AL\Db\bind_result(
72
            "IndividualDAO: getIndividualsStartingWith",
73
            $stmt,
74
            $id,
75
            $name
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
76
        );
77
78
        $individuals = [];
79
        while ($stmt->fetch()) {
80
            $individuals[] = new \AL\Common\Model\Individual\Individual(
81
                $id,
82
                $name
83
            );
84
        }
85
86
        $stmt->close();
87
88
        return $individuals;
89
    }
90
91
    /**
92
     * Get a specific individual
93
     * @param number $id ID of the individual to retrieve
94
     * @return \AL\Common\Model\Individual\Individual The individual, or NULL if not found
95
     */
96
    public function getIndividual($id) {
97
        $stmt = \AL\Db\execute_query(
98
            "IndividualDAO: getIndividual: $id",
99
            $this->mysqli,
100
            "SELECT ind_id, ind_name FROM individuals WHERE ind_id = ?",
101
            "i",
102
            $id
103
        );
104
105
        \AL\Db\bind_result(
106
            "IndividualDAO: getIndividual: $id",
107
            $stmt,
108
            $id,
109
            $name
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
110
        );
111
112
        $individual = null;
113
        if ($stmt->fetch()) {
114
            $individual = new \AL\Common\Model\Individual\Individual(
115
                $id,
116
                $name
117
            );
118
        }
119
120
        $stmt->close();
121
122
        return $individual;
123
    }
124
125
    /**
126
     * Add new individual
127
     * @param number $new_ind_name Name of new indivudal
128
     * @return new primary key
129
     */
130
    public function addIndividual($new_ind_name) {
131
        $stmt = \AL\Db\execute_query(
132
            "IndividualDAO: addIndividual: $new_ind_name",
133
            $this->mysqli,
134
            "INSERT INTO individuals (ind_name) VALUES (?)",
135
            "s",
136
            $new_ind_name
137
        );
138
139
        $new_ind_id = $stmt->insert_id;
140
141
        $stmt->close();
142
143
        return $new_ind_id;
144
    }
145
}
146