CrewDAO   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 87
dl 0
loc 176
rs 10
c 0
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getCrewsForRelease() 0 30 2
A getCrew() 0 27 2
A __construct() 0 2 1
A getAllCrews() 0 2 1
A getCrewsStartingWith() 0 34 4
A addCrew() 0 14 1
A addCrewToRelease() 0 9 1
A deleteCrewFromRelease() 0 9 1
1
<?php
2
namespace AL\Common\DAO;
3
4
require_once __DIR__."/../../lib/Db.php";
5
require_once __DIR__."/../Model/Crew/Crew.php";
6
7
/**
8
 * DAO for Crew
9
 */
10
class CrewDAO {
11
    private $mysqli;
12
13
    public function __construct($mysqli) {
14
        $this->mysqli = $mysqli;
15
    }
16
    
17
    public function getAllCrews() {
18
        return $this->getCrewsStartingWith(".");
19
    }
20
21
    /**
22
     * Search for crews
23
     * @param string $search_string can either be a single letter or "num" which represents numbers
24
     * @return \AL\Common\Model\Crew\Crew[] A list of crews
25
     */
26
    public function getCrewsStartingWith($search_string) {
27
        if (isset($search_string) and $search_string == "num") {
28
            $search_string = "^[0-9]";
29
        } else {
30
            $search_string = "^$search_string";
31
        }
32
33
        $stmt = \AL\Db\execute_query(
34
            "CrewDAO: getCrewsStartingWith",
35
            $this->mysqli,
36
            "SELECT crew_id, crew_name FROM crew
37
            WHERE LOWER(crew_name) REGEXP ? ORDER BY crew_name ASC",
38
            "s",
39
            strtolower($search_string)
40
        );
41
42
        \AL\Db\bind_result(
43
            "CrewDAO: getCrewsStartingWith",
44
            $stmt,
45
            $crew_id,
46
            $crew_name
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $crew_name seems to be never defined.
Loading history...
47
        );
48
49
        $crews = [];
50
        while ($stmt->fetch()) {
51
            $crews[] = new \AL\Common\Model\Crew\Crew(
52
                $crew_id,
53
                $crew_name
54
            );
55
        }
56
57
        $stmt->close();
58
59
        return $crews;
60
    }
61
62
    /**
63
     * Get a specific crew
64
     * @param number $crew_id ID of the crew to retrieve
65
     * @return \AL\Common\Model\Crew\Crew The crew, or NULL if not found
66
     */
67
    public function getCrew($crew_id) {
68
        $stmt = \AL\Db\execute_query(
69
            "CrewDAO: getCrew: $crew_id",
70
            $this->mysqli,
71
            "SELECT crew_id, crew_name FROM crew WHERE crew_id = ?",
72
            "i",
73
            $crew_id
74
        );
75
76
        \AL\Db\bind_result(
77
            "CrewDAO: getCrew: $crew_id",
78
            $stmt,
79
            $crew_id,
80
            $crew_name
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $crew_name seems to be never defined.
Loading history...
81
        );
82
83
        $crew = null;
84
        if ($stmt->fetch()) {
85
            $crew = new \AL\Common\Model\Crew\Crew(
86
                $crew_id,
87
                $crew_name
88
            );
89
        }
90
91
        $stmt->close();
92
93
        return $crew;
94
    }
95
96
    /**
97
     * Add new Crew
98
     * @param string $new_crew_name Name of the new crew to add
99
     * @return \AL\Common\Model\Crew\Crew The crew, or NULL if not found
100
     */
101
    public function addCrew($new_crew_name) {
102
        $stmt = \AL\Db\execute_query(
103
            "CrewDAO: addCrew: $new_crew_name",
104
            $this->mysqli,
105
            "INSERT INTO crew (crew_name) VALUES (?)",
106
            "s",
107
            $new_crew_name
108
        );
109
110
        $new_crew_id = $stmt->insert_id;
111
112
        $stmt->close();
113
114
        return $new_crew_id;
115
    }
116
    
117
    /**
118
     * Get list of crews for a game_release
119
     *
120
     * @param integer release ID
0 ignored issues
show
Bug introduced by
The type AL\Common\DAO\release was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
121
     */
122
    public function getCrewsForRelease($game_release_id) {
123
        $stmt = \AL\Db\execute_query(
124
            "CrewsDAO: getCrewsForRelease",
125
            $this->mysqli,
126
            "SELECT
127
                crew.crew_id,
128
                crew.crew_name
129
            FROM
130
                crew
131
            LEFT JOIN game_release_crew ON crew.crew_id = game_release_crew.crew_id
132
            WHERE game_release_crew.game_release_id = ?",
133
            "i", $game_release_id
134
        );
135
136
        \AL\Db\bind_result(
137
            "CrewsDAO: getCrewsForRelease",
138
            $stmt,
139
            $id, $name
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
140
        );
141
142
        $crews = [];
143
        while ($stmt->fetch()) {
144
            $crews[] = new \AL\Common\Model\Crew\Crew(
145
                $id, $name
146
            );
147
        }
148
149
        $stmt->close();
150
151
        return $crews;
152
    }
153
    
154
     /**
155
     * add a crew to a release
156
     *
157
     * @param integer release ID
158
     * @param integer crew ID
159
     */
160
    public function addCrewToRelease($release_id, $crew_id) {
161
        $stmt = \AL\Db\execute_query(
162
            "CrewDAO: addCrewToRelease",
163
            $this->mysqli,
164
            "INSERT INTO game_release_crew (game_release_id, crew_id) VALUES (?, ?)",
165
            "ii", $release_id, $crew_id
166
        );
167
168
        $stmt->close();
169
    }
170
        
171
    /**
172
     * delete a crew from a release
173
     *
174
     * @param int release_id
175
     * @param int crew_id
0 ignored issues
show
Bug introduced by
The type AL\Common\DAO\crew_id was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
176
     */
177
    public function deleteCrewFromRelease($release_id, $crew_id) {
178
        $stmt = \AL\Db\execute_query(
179
            "CrewDAO: deleteCrewFromRelease",
180
            $this->mysqli,
181
            "DELETE FROM game_release_crew WHERE game_release_id = ? and crew_id = ?",
182
            "ii", $release_id, $crew_id
183
        );
184
185
        $stmt->close();
186
    }
187
}
188