GameSeriesDAO::updateGameSeries()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
namespace AL\Common\DAO;
3
4
require_once __DIR__."/../../lib/Db.php" ;
5
require_once __DIR__."/../Model/Game/GameSeries.php" ;
6
require_once __DIR__."/../Model/Game/Game.php" ;
7
8
/**
9
 * DAO for GamesSeries
10
 */
11
class GameSeriesDAO {
12
    private $mysqli;
13
14
    public function __construct($mysqli) {
15
        $this->mysqli = $mysqli;
16
    }
17
18
    /**
19
     * Get all game series
20
     * @return \AL\Common\Model\Game\GameSeries[] All game series
21
     */
22
    public function getAllGameSeries() {
23
        $stmt = \AL\Db\execute_query(
24
            "GameSeriesDAO: getAllGameSeries",
25
            $this->mysqli,
26
            "SELECT id, name FROM game_series ORDER BY name ASC",
27
            null, null
28
        );
29
30
        \AL\Db\bind_result(
31
            "GameSeriesDAO: getAllGameSeries",
32
            $stmt,
33
            $id, $name
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
34
        );
35
36
        $game_series = [];
37
        while ($stmt->fetch()) {
38
            $game_series[] = new \AL\Common\Model\Game\GameSeries($id, $name);
39
        }
40
41
        $stmt->close();
42
43
        return $game_series;
44
    }
45
46
    /**
47
     * Get a single game series
48
     * @param number $game_series_id ID of the game series to retrieve
49
     * @return \AL\Common\Model\Game\GameSeries The game series
50
     */
51
    public function getGameSeries($game_series_id) {
52
        $stmt = \AL\Db\execute_query(
53
            "GameSeriesDAO: getGameSeries: $game_series_id",
54
            $this->mysqli,
55
            "SELECT id, name FROM game_series WHERE id = ?",
56
            "i", $game_series_id
57
        );
58
59
        \AL\Db\bind_result(
60
            "GameSeriesDAO: getGameSeries: $game_series_id",
61
            $stmt,
62
            $id, $name
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
63
        );
64
65
        $game_series = null;
66
        if ($stmt->fetch()) {
67
            $game_series = new \AL\Common\Model\Game\GameSeries($id, $name);
68
        }
69
70
        $stmt->close();
71
72
        return $game_series;
73
    }
74
75
    /**
76
     * Get all the games for a series
77
     * @param number $game_series_id ID of series to get games from
78
     * @return \AL\Common\Model\Game\Game[] List of games
79
     */
80
    public function getGamesForSeries($game_series_id) {
81
        $stmt = \AL\Db\execute_query(
82
            "GameSeriesDAO: getGamesForSeries: $game_series_id",
83
            $this->mysqli,
84
            "SELECT game_id, game_name, game_series_id
85
            FROM game
86
            WHERE game_series_id = ?
87
            ORDER BY game_name ASC",
88
            "i", $game_series_id
89
        );
90
91
        \AL\Db\bind_result(
92
            "GameSeriesDAO: getGamesForSeries: $game_series_id",
93
            $stmt,
94
            $game_id, $game_name, $game_series_id
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $game_name seems to be never defined.
Loading history...
95
        );
96
97
        $games = [];
98
        while ($stmt->fetch()) {
99
            $games[] = new \AL\Common\Model\Game\Game(
100
                $game_id, $game_name, $game_series_id
101
            );
102
        }
103
104
        $stmt->close();
105
106
        return $games;
107
    }
108
109
    /**
110
     * Update a game series
111
     *
112
     * @param integer $id ID of the series to update
113
     * @param string $name New name of the series
114
     */
115
    public function updateGameSeries($id, $name) {
116
        $stmt = \AL\Db\execute_query(
117
            "GameSeriesDAO: updateGameSeries: $id",
118
            $this->mysqli,
119
            "UPDATE game_series
120
            SET `name` = ?
121
            WHERE id = ?",
122
            "si", $name, $id
123
        );
124
125
        $stmt->close();
126
    }
127
128
    /**
129
     * Delete a game series
130
     *
131
     * @param integer $id ID of the series to delete
132
     */
133
    public function deleteGameSeries($id) {
134
        // Unlink all games from this series
135
        $stmt = \AL\Db\execute_query(
136
            "GameSeriesDAO: deleteGameSeries: $id",
137
            $this->mysqli,
138
            "UPDATE game SET game_series_id = NULL WHERE game_series_id = ?",
139
            "i", $id
140
        );
141
142
        $stmt->close();
143
144
        $stmt = \AL\Db\execute_query(
145
            "GameSeriesDAO: deleteGameSeries: $id",
146
            $this->mysqli,
147
            "DELETE FROM game_series WHERE id = ?",
148
            "i", $id
149
        );
150
151
        $stmt->close();
152
    }
153
154
    /**
155
     * Insert a new series
156
     *
157
     * @param \AL\Common\Model\Game\GameSeries $game_series Series to add
158
     * @return integer ID of the inserted game series
159
     */
160
    public function addGameSeries($game_series) {
161
        $stmt = \AL\Db\execute_query(
162
            "GameSeriesDAO: addGameSeries",
163
            $this->mysqli,
164
            "INSERT INTO game_series (name) VALUES (?)",
165
            "s", $game_series->getName()
166
        );
167
168
        $id = $stmt->insert_id;
169
170
        $stmt->close();
171
172
        return $id;
173
    }
174
175
    /**
176
     * Add a game to a series
177
     *
178
     * @param integer $series_id ID of the series
179
     * @param integer $game_id ID of the game
180
     */
181
    public function addGameToSeries($series_id, $game_id) {
182
        $stmt = \AL\Db\execute_query(
183
            "GameSeriesDAO: addGameToSeries",
184
            $this->mysqli,
185
            "UPDATE game SET game_series_id = ? WHERE game_id = ?",
186
            "ii", $series_id, $game_id
187
        );
188
189
        $stmt->close();
190
    }
191
192
    /**
193
     * Remove a game from a series
194
     *
195
     * @param integer $game_id IDs of the game to remove
196
     */
197
    public function removeGameFromSeries($game_id) {
198
        $stmt = \AL\Db\execute_query(
199
            "GameSeriesDAO: removeGameFromSeries",
200
            $this->mysqli,
201
            "UPDATE game
202
            SET game_series_id = NULL
203
            WHERE game_id = ?",
204
            "i", $game_id
205
        );
206
207
        $stmt->close();
208
    }
209
}
210