GameDAO   B
last analyzed

Complexity

Total Complexity 45

Size/Duplication

Total Lines 429
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 210
dl 0
loc 429
rs 8.8
c 0
b 0
f 0
wmc 45

15 Methods

Rating   Name   Duplication   Size   Complexity  
A removePublisher() 0 30 5
A addIndividual() 0 18 3
A getGame() 0 27 2
A removeIndividual() 0 20 3
A addDeveloper() 0 11 2
A updateDeveloper() 0 31 4
A getMultiplayerTypes() 0 4 1
A updateIndividual() 0 29 5
A getMultiplayerHardware() 0 4 1
A getRandomScreenshot() 0 32 4
A __construct() 0 2 1
B updatePublisher() 0 42 7
A updateGameMultiplayer() 0 21 1
A addPublisher() 0 12 3
A removeDeveloper() 0 22 3

How to fix   Complexity   

Complex Class

Complex classes like GameDAO 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.

While breaking up the class, it is a good idea to analyze how other classes use GameDAO, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace AL\Common\DAO;
3
4
require_once __DIR__."/../../lib/Db.php" ;
5
require_once __DIR__."/../Model/Game/Game.php" ;
6
7
/**
8
 * DAO for Games
9
 */
10
class GameDAO {
11
    private $mysqli;
12
    
13
    const MULTIPLAY_TYPE_SIMULTANEOUS = 'Simultaneous';
14
    const MULTIPLAY_TYPE_TURN_BY_TURN = 'Turn by turn';
15
    
16
    const MULTIPLAY_HARDWARE_CARTRIDGE = 'Cartridge';
17
    const MULTIPLAY_HARDWARE_MIDI_LINK = 'Midi-Link';
18
19
    public function __construct($mysqli) {
20
        $this->mysqli = $mysqli;
21
    }
22
    
23
    /**
24
     * Get all the multiplayer types
25
     * @return String[] A list of multiplayer types
26
     */
27
    public function getMultiplayerTypes() {
28
        return array(
29
            GameDAO::MULTIPLAY_TYPE_SIMULTANEOUS,
30
            GameDAO::MULTIPLAY_TYPE_TURN_BY_TURN
31
        );
32
    }
33
    
34
    /**
35
     * Get all the multiplayer hardware
36
     * @return String[] A list of multiplayer hardware
37
     */
38
    public function getMultiplayerHardware() {
39
        return array(
40
            GameDAO::MULTIPLAY_HARDWARE_CARTRIDGE,
41
            GameDAO::MULTIPLAY_HARDWARE_MIDI_LINK
42
        );
43
    }
44
45
    /**
46
     * Get a single game
47
     * @param number $game_id ID of the game to retrieve
48
     * @return \AL\Common\Model\Game\Game The game
49
     */
50
    public function getGame($game_id) {
51
        $stmt = \AL\Db\execute_query(
52
            "GameDAO: getGame: $game_id",
53
            $this->mysqli,
54
            "SELECT game_id, game_name, game_series_id, number_players_on_same_machine,
55
                number_players_multiple_machines, multiplayer_type, multiplayer_hardware FROM game WHERE game_id = ?",
56
            "i", $game_id
57
        );
58
59
        \AL\Db\bind_result(
60
            "GameDAO: getGame: $game_id",
61
            $stmt,
62
            $game_id, $game_name, $game_series_id, $number_players_on_same_machine, $number_players_multiple_machines,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $number_players_multiple_machines seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $number_players_on_same_machine seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $game_name seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $game_series_id seems to be never defined.
Loading history...
63
            $multiplayer_type, $multiplayer_hardware
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $multiplayer_hardware seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $multiplayer_type seems to be never defined.
Loading history...
64
        );
65
66
        $game = null;
67
        if ($stmt->fetch()) {
68
            $game = new \AL\Common\Model\Game\Game(
69
                $game_id, $game_name, $game_series_id, $number_players_on_same_machine,
70
                $number_players_multiple_machines, $multiplayer_type, $multiplayer_hardware
71
            );
72
        }
73
74
        $stmt->close();
75
76
        return $game;
77
    }
78
    
79
    /**
80
     * Update the multiplayer attributes of a game
81
     *
82
     * @param integer $release_id ID of the release to update
83
     */
84
    public function updateGameMultiplayer(
85
        $game_id,
86
        $players_same,
87
        $players_other,
88
        $multiplayer_type,
89
        $multiplayer_hardware
90
    ) {
91
        $stmt = \AL\Db\execute_query(
92
            "GameDAO: updateGameMultiplayer",
93
            $this->mysqli,
94
            "UPDATE game
95
            SET
96
                `number_players_on_same_machine` = ?,
97
                `number_players_multiple_machines` = ?,
98
                `multiplayer_type` = ?,
99
                `multiplayer_hardware` = ?               
100
            WHERE game_id = ?",
101
            "iissi", $players_same, $players_other, $multiplayer_type, $multiplayer_hardware, $game_id
102
        );
103
104
        $stmt->close();
105
    }
106
 
107
    /**
108
     * Get a random screenshot for a game
109
     * @param number $game_id ID of the game to get a screenshot for
110
     * @return String The relative URL of a screenshot
111
     */
112
    public function getRandomScreenshot($game_id) {
113
        $stmt = \AL\Db\execute_query(
114
            "GameDAO: getRandomScreenshot: $game_id",
115
            $this->mysqli,
116
            "SELECT screenshot_game.screenshot_id, imgext FROM screenshot_game
117
            LEFT JOIN screenshot_main ON (screenshot_game.screenshot_id  = screenshot_main.screenshot_id)
118
            WHERE screenshot_game.game_id = ?
119
            ORDER BY RAND() LIMIT 1",
120
            "i", $game_id
121
        );
122
123
        \AL\Db\bind_result(
124
            "GameDAO: getRandomScreenshot: $game_id",
125
            $stmt,
126
            $screenshot_id, $imgext
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $imgext seems to be never defined.
Loading history...
127
        );
128
129
        $screenshot = null;
130
        if ($stmt->fetch()) {
131
            if ($screenshot_id == null) {
132
                $screenshot = null;
133
            } else {
134
                $screenshot = $screenshot_id.".".$imgext;
135
            }
136
        }
137
138
        $stmt->close();
139
140
        if ($screenshot == null) {
141
            return $screenshot;
142
        } else {
143
            return $GLOBALS['game_screenshot_path']."/".$screenshot;
144
        }
145
    }
146
147
    /**
148
     * Remove an individual from a game
149
     * @param number $game_id ID of the game to remove the individual from
150
     * @param number $individual_id ID of the individual to remove
151
     * @param number $individual_role_id ID of the type of individual to remove
152
     */
153
    public function removeIndividual($game_id, $individual_id, $individual_role_id) {
154
        if ($individual_role_id != null && $individual_role_id != '') {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $individual_role_id of type double|integer against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
155
            $stmt = \AL\Db\execute_query(
156
                "GameDAO: removeIndividual",
157
                $this->mysqli,
158
                "DELETE FROM game_individual
159
                WHERE game_id = ? AND individual_id = ? AND individual_role_id = ?",
160
                "iii", $game_id, $individual_id, $individual_role_id
161
            );
162
        } else {
163
            $stmt = \AL\Db\execute_query(
164
                "GameDAO: removeIndividual",
165
                $this->mysqli,
166
                "DELETE FROM game_individual
167
                WHERE game_id = ? AND individual_id = ?",
168
                "ii", $game_id, $individual_id
169
            );
170
        }
171
172
        $stmt->close();
173
    }
174
175
    /**
176
     * Add a new individual to a game
177
     * @param number $game_id ID of the game to add the individual to
178
     * @param number $individual_id ID of the individual to add
179
     * @param number individual_role_id ID of the type of individual to add
0 ignored issues
show
Bug introduced by
The type AL\Common\DAO\individual_role_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...
180
     */
181
    public function addIndividual($game_id, $individual_id, $individual_role_id) {
182
        if ($individual_role_id == null && $individual_role_id == '') {
183
            $stmt = \AL\Db\execute_query(
184
                "GameDAO: addIndividual",
185
                $this->mysqli,
186
                "INSERT INTO game_individual (game_id, individual_id, individual_role_id) VALUES (?, ?, null)",
187
                "ii", $game_id, $individual_id
188
            );
189
        } else {
190
            $stmt = \AL\Db\execute_query(
191
                "GameDAO: addIndividual",
192
                $this->mysqli,
193
                "INSERT INTO game_individual (game_id, individual_id, individual_role_id) VALUES (?, ?, ?)",
194
                "iii", $game_id, $individual_id, $individual_role_id
195
            );
196
        }
197
198
        $stmt->close();
199
    }
200
201
    /**
202
     * Update the individual role on a game
203
     * @param number $game_id ID of the game to update the individual for
204
     * @param number $individual_id ID of the individual to update the type for
205
     * @param number individual_role_id Previous individual role ID
206
     * @param number $new_individual_role_id New individual role ID
207
     */
208
    public function updateIndividual($game_id, $individual_id, $individual_role_id, $new_individual_role_id) {
209
        if ($new_individual_role_id == null && $new_individual_role_id == '') {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $new_individual_role_id of type double|integer against null; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
210
            $query = "UPDATE game_individual SET individual_role_id = null
211
                WHERE game_id = ? AND individual_id = ? ";
212
            $bind_string = "ii";
213
            $bind_params = array($game_id, $individual_id);
214
        } else {
215
            $query = "UPDATE game_individual SET individual_role_id = ?
216
                WHERE game_id = ? AND individual_id = ? ";
217
            $bind_string = "iii";
218
            $bind_params = array($new_individual_role_id, $game_id, $individual_id);
219
        }
220
221
        if ($individual_role_id != null && $individual_role_id != '') {
222
            $query .= "AND individual_role_id = ?";
223
            $bind_string .= "i";
224
            $bind_params[] = $individual_role_id;
225
        } else {
226
            $query .= "AND individual_role_id IS NULL";
227
        }
228
229
        $stmt = \AL\Db\execute_query(
230
            "GameDAO: updateIndividual",
231
            $this->mysqli,
232
            $query,
233
            $bind_string, ...$bind_params
234
        );
235
236
        $stmt->close();
237
    }
238
239
    /**
240
     * Remove a developer from a game
241
     * @param number $game_id ID of the game to remove the developer from
242
     * @param number $pub_dev_id ID of the developer to remove
243
     * @param number $developer_role_id ID of the developer role to remove
244
     */
245
    public function removeDeveloper($game_id, $pub_dev_id, $developer_role_id) {
246
        $query = "DELETE FROM game_developer WHERE game_id = ? AND dev_pub_id = ?";
247
        $bind_string = "ii";
248
        $bind_params = array($game_id, $pub_dev_id);
249
250
        if ($developer_role_id != null && $developer_role_id != '') {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $developer_role_id of type double|integer against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
251
            $query .= " AND developer_role_id = ?";
252
            $bind_string .= "i";
253
            $bind_params[] = $developer_role_id;
254
        } else {
255
            $query .= " AND developer_role_id IS NULL";
256
        }
257
258
259
        $stmt = \AL\Db\execute_query(
260
            "GameDAO: removeDeveloper",
261
            $this->mysqli,
262
            $query,
263
            $bind_string, ...$bind_params
264
        );
265
266
        $stmt->close();
267
    }
268
269
    /**
270
     * Add a developer to a game
271
     * @param number $game_id ID of the game to add the developer to
272
     * @param number $pub_dev_id ID of the developer to add
273
     * @param number $developer_role_id ID of the developer role to add
274
     */
275
    public function addDeveloper($game_id, $pub_dev_id, $developer_role_id) {
276
        $stmt = \AL\Db\execute_query(
277
            "GameDAO: addDeveloper",
278
            $this->mysqli,
279
            "INSERT INTO game_developer (game_id, dev_pub_id, developer_role_id)
280
            VALUES (?, ?, ?)",
281
            "iii", $game_id, $pub_dev_id,
282
            $developer_role_id == '' ? null : $developer_role_id
283
        );
284
285
        $stmt->close();
286
    }
287
288
    /**
289
     * Update the developer on a game
290
     * @param number $game_id ID of the game to update the developer for
291
     * @param number $pub_dev_id ID of the developer to update
292
     * @param number $developer_role_id ID of the developer role to update
293
     * @param number $new_developer_role_id New ID of the developer role to update
294
     */
295
    public function updateDeveloper(
296
        $game_id,
297
        $pub_dev_id,
298
        $developer_role_id,
299
        $new_developer_role_id
300
    ) {
301
302
        $query = "UPDATE game_developer SET developer_role_id = ?
303
            WHERE game_id = ? AND dev_pub_id = ?";
304
        $bind_string = "iii";
305
        $bind_params = array(
306
            $new_developer_role_id != '' ? $new_developer_role_id : null,
307
            $game_id,
308
            $pub_dev_id);
309
310
        if ($developer_role_id != null && $developer_role_id != '') {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $developer_role_id of type double|integer against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
311
            $query .= " AND developer_role_id = ?";
312
            $bind_string .= "i";
313
            $bind_params[] = $developer_role_id;
314
        } else {
315
            $query .= " AND developer_role_id IS NULL";
316
        }
317
318
        $stmt = \AL\Db\execute_query(
319
            "GameDAO: updateDeveloper",
320
            $this->mysqli,
321
            $query,
322
            $bind_string, ...$bind_params
323
        );
324
325
        $stmt->close();
326
    }
327
328
    /**
329
     * Remove a publisher from a game
330
     * @param number $game_id ID of the game to remove the publisher from
331
     * @param number $pub_dev_id ID of the publisher to remove
332
     * @param number $continent_id ID of the continent to remove
333
     * @param number $game_extra_info_id ID of the extra info to remove
334
     */
335
    public function removePublisher($game_id, $pub_dev_id, $continent_id, $game_extra_info_id) {
336
        $query = "DELETE FROM game_publisher WHERE game_id = ? AND pub_dev_id = ?";
337
        $bind_string = "ii";
338
        $bind_params = array($game_id, $pub_dev_id);
339
340
        if ($continent_id != null && $continent_id != '') {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $continent_id of type double|integer against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
341
            $query .= " AND continent_id = ?";
342
            $bind_string .= "i";
343
            $bind_params[] = $continent_id;
344
        } else {
345
            $query .= " AND continent_id IS NULL";
346
        }
347
348
        if ($game_extra_info_id != null && $game_extra_info_id != '') {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $game_extra_info_id of type double|integer against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
349
            $query .= " AND game_extra_info_id = ?";
350
            $bind_string .= "i";
351
            $bind_params[] = $game_extra_info_id;
352
        } else {
353
            $query .= " AND game_extra_info_id IS NULL";
354
        }
355
356
357
        $stmt = \AL\Db\execute_query(
358
            "GameDAO: removePublisher",
359
            $this->mysqli,
360
            $query,
361
            $bind_string, ...$bind_params
362
        );
363
364
        $stmt->close();
365
    }
366
367
    /**
368
     * Add a publisher to a game
369
     * @param number $game_id ID of the game to add the publisher to
370
     * @param number $pub_dev_id ID of the publisher to add
371
     * @param number $continent_id ID of the continent to add
372
     * @param number $game_extra_info_id ID of the extra info to add
373
     */
374
    public function addPublisher($game_id, $pub_dev_id, $continent_id, $game_extra_info_id) {
375
        $stmt = \AL\Db\execute_query(
376
            "GameDAO: addPublisher",
377
            $this->mysqli,
378
            "INSERT INTO game_publisher (game_id, pub_dev_id, continent_id, game_extra_info_id)
379
            VALUES (?, ?, ?, ?)",
380
            "iiii", $game_id, $pub_dev_id,
381
            $continent_id == '' ? null : $continent_id,
382
            $game_extra_info_id == '' ? null : $game_extra_info_id
383
        );
384
385
        $stmt->close();
386
    }
387
388
    /**
389
     * Update the publisher on a game
390
     * @param number $game_id ID of the game to update the publisher for
391
     * @param number $pub_dev_id ID of the publisher to update
392
     * @param number $continent_id ID of the continent to update
393
     * @param number $game_extra_info_id ID of the extra info to update
394
     * @param number $new_continent_id New ID of the continent to update
395
     * @param number $new_game_extra_info_id New ID of the extra info to update
396
     */
397
    public function updatePublisher(
398
        $game_id,
399
        $pub_dev_id,
400
        $continent_id,
401
        $game_extra_info_id,
402
        $new_continent_id,
403
        $new_game_extra_info_id
404
    ) {
405
406
        $query = "UPDATE game_publisher SET continent_id = ?, game_extra_info_id = ?
407
            WHERE game_id = ? AND pub_dev_id = ?";
408
        $bind_string = "iiii";
409
        $bind_params = array(
410
            $new_continent_id != '' ? $new_continent_id : null,
411
            $new_game_extra_info_id != '' ? $new_game_extra_info_id : null,
412
            $game_id,
413
            $pub_dev_id);
414
415
        if ($continent_id != null && $continent_id != '') {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $continent_id of type double|integer against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
416
            $query .= " AND continent_id = ?";
417
            $bind_string .= "i";
418
            $bind_params[] = $continent_id;
419
        } else {
420
            $query .= " AND continent_id IS NULL";
421
        }
422
423
        if ($game_extra_info_id != null && $game_extra_info_id != '') {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $game_extra_info_id of type double|integer against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
424
            $query .= " AND game_extra_info_id = ?";
425
            $bind_string .= "i";
426
            $bind_params[] = $game_extra_info_id;
427
        } else {
428
            $query .= " AND game_extra_info_id IS NULL";
429
        }
430
431
        $stmt = \AL\Db\execute_query(
432
            "GameDAO: updatePublisher",
433
            $this->mysqli,
434
            $query,
435
            $bind_string, ...$bind_params
436
        );
437
438
        $stmt->close();
439
    }
440
}
441