CommentsDAO::getTemporaryTableCommentsQuery()   B
last analyzed

Complexity

Conditions 11
Paths 40

Size

Total Lines 48
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 20
nc 40
nop 3
dl 0
loc 48
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace AL\Common\DAO;
3
4
require_once __DIR__."/../../lib/Db.php";
5
require_once __DIR__."/../Model/Comments/Comments.php";
6
7
/**
8
 * DAO for Comments
9
 */
10
class CommentsDAO {
11
    private $mysqli;
12
13
    public function __construct($mysqli) {
14
        $this->mysqli = $mysqli;
15
    }
16
17
    /**
18
     * Get the SQL query to get game comments, either all or comments for a
19
     * specific user
20
     *
21
     * @param  integer $user_id Optional ID of the cuser to retrieve comments for
22
     * @return string SQL query
23
     */
24
    public function getGameCommentQuery($user_id = null) {
25
        $query = "SELECT
26
                comments.comments_id,
27
                comments.timestamp,
28
                users.user_id,
29
                (SELECT COUNT(*) FROM comments WHERE comments.user_id = users.user_id) AS user_comment_count,
30
                (SELECT COUNT(*) FROM game_submitinfo WHERE game_submitinfo.user_id = users.user_id) AS user_subm_count,
31
                users.userid,
32
                users.email,
33
                users.join_date,
34
                users.karma,
35
                users.show_email,
36
                users.avatar_ext,
37
                game.game_id,
38
                game.game_name,
39
                'game_comment' AS comment_type,
40
                null
41
            FROM game_user_comments
42
            LEFT JOIN comments
43
                ON ( game_user_comments.comment_id = comments.comments_id )
44
            LEFT JOIN users
45
                ON ( comments.user_id = users.user_id )
46
            LEFT JOIN game
47
                ON ( game_user_comments.game_id = game.game_id )";
48
49
        if (isset($user_id)) {
50
            $query .= " WHERE users.user_id = $user_id";
51
        }
52
53
        return $query;
54
    }
55
56
    /**
57
     * Get the SQL query to get game review comments, either all or comments for a
58
     * specific user
59
     *
60
     * @param  integer $user_id Optional ID of the cuser to retrieve comments for
61
     * @return string SQL query
62
     */
63
    public function getGameReviewCommentQuery($user_id = null) {
64
        $query = "SELECT
65
                comments.comments_id,
66
                comments.timestamp,
67
                users.user_id,
68
                (SELECT COUNT(*) FROM comments WHERE comments.user_id = users.user_id) AS user_comment_count,
69
                (SELECT COUNT(*) FROM game_submitinfo WHERE game_submitinfo.user_id = users.user_id) AS user_subm_count,
70
                users.userid,
71
                users.email,
72
                users.join_date,
73
                users.karma,
74
                users.show_email,
75
                users.avatar_ext,
76
                game.game_id AS game_id,
77
                game.game_name AS game_name,
78
                'game_review_comment' AS comment_type,
79
                review_game.review_id as review_id
80
            FROM review_user_comments
81
            LEFT JOIN comments
82
                ON ( review_user_comments.comment_id = comments.comments_id )
83
            LEFT JOIN review_game
84
                ON (review_user_comments.review_id = review_game.review_id )
85
            LEFT JOIN game
86
                ON ( review_game.game_id = game.game_id )
87
            LEFT JOIN users
88
                ON ( comments.user_id = users.user_id )";
89
90
        if (isset($user_id)) {
91
            $query .= " WHERE users.user_id = $user_id";
92
        }
93
94
        return $query;
95
    }
96
97
    /**
98
     * Get the SQL query to get interview comments, either all or comments for a
99
     * specific user
100
     *
101
     * @param  integer $user_id Optional ID of the user to retrieve comments for
102
     * @return string SQL query
103
     */
104
    public function getInterviewCommentQuery($user_id = null) {
105
        $query = "SELECT
106
                comments.comments_id,
107
                comments.timestamp,
108
                users.user_id,
109
                (SELECT COUNT(*) FROM comments WHERE comments.user_id = users.user_id) AS user_comment_count,
110
                (SELECT COUNT(*) FROM game_submitinfo WHERE game_submitinfo.user_id = users.user_id) AS user_subm_count,
111
                users.userid,
112
                users.email,
113
                users.join_date,
114
                users.karma,
115
                users.show_email,
116
                users.avatar_ext,
117
                interview_main.interview_id AS game_id,
118
                individuals.ind_name AS game_name,
119
                'interview_comment' AS comment_type,
120
                null
121
            FROM interview_user_comments
122
            LEFT JOIN comments
123
                ON ( interview_user_comments.comment_id = comments.comments_id )
124
            LEFT JOIN interview_main
125
                ON (interview_user_comments.interview_id = interview_main.interview_id )
126
            LEFT JOIN individuals
127
                ON (interview_main.ind_id = individuals.ind_id )
128
            LEFT JOIN users
129
                ON ( comments.user_id = users.user_id )";
130
131
        if (isset($user_id)) {
132
            $query .= " WHERE users.user_id = $user_id";
133
        }
134
135
        return $query;
136
    }
137
    
138
    /**
139
     * Get the SQL query to get article comments, either all or comments for a
140
     * specific user
141
     *
142
     * @param  integer $user_id Optional ID of the user to retrieve comments for
143
     * @return string SQL query
144
     */
145
    public function getArticleCommentQuery($user_id = null) {
146
        $query = "SELECT
147
                comments.comments_id,
148
                comments.timestamp,
149
                users.user_id,
150
                (SELECT COUNT(*) FROM comments WHERE comments.user_id = users.user_id) AS user_comment_count,
151
                (SELECT COUNT(*) FROM game_submitinfo WHERE game_submitinfo.user_id = users.user_id) AS user_subm_count,
152
                users.userid,
153
                users.email,
154
                users.join_date,
155
                users.karma,
156
                users.show_email,
157
                users.avatar_ext,
158
                article_main.article_id AS game_id,
159
                article_text.article_title AS game_name,
160
                'article_comment' AS comment_type,
161
                null
162
            FROM article_user_comments
163
            LEFT JOIN comments
164
                ON ( article_user_comments.comments_id = comments.comments_id )
165
            LEFT JOIN article_main
166
                ON ( article_user_comments.article_id = article_main.article_id )
167
            LEFT JOIN article_text
168
                ON ( article_main.article_id = article_text.article_id )
169
            LEFT JOIN users
170
                ON ( comments.user_id = users.user_id )";
171
172
        if (isset($user_id)) {
173
            $query .= " WHERE users.user_id = $user_id";
174
        }
175
176
        return $query;
177
    }
178
179
    /**
180
     * Get the SQL query to create a temporary table to hold the comments
181
     *
182
     * @param  integer $mysqli Optional ID of the user to retrieve comments for
183
     * @return string SQL query
184
     */
185
    private function createTemporaryTableCommentsQuery($mysqli = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $mysqli is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

185
    private function createTemporaryTableCommentsQuery(/** @scrutinizer ignore-unused */ $mysqli = null) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
186
        $query = "CREATE TEMPORARY TABLE temp (
187
            comments_id int(11),
188
            timestamp varchar(32),
189
            user_id int(11),
190
            user_comment_count int(11),
191
            user_subm_count int(11),
192
            userid varchar(255),
193
            email varchar(255),
194
            join_date varchar(32),
195
            karma int(11),
196
            show_email int(1),
197
            avatar_ext varchar(255),
198
            game_id int(11),
199
            game_name varchar(255),
200
            comment_type varchar(255),
201
            review_id int(11)
202
        )";
203
204
        return $query;
205
    }
206
207
    /**
208
     * Get the SQL query to create a temporary table to hold the comments
209
     *
210
     * @param  string $view Optional string to id what kind of list of comments to be rendered
211
     * @param  string $action Optional string to id scroll autoload
212
     * @param  integer $last_timestamp timestamp used for the autoload
213
     * @return string SQL query
214
     */
215
    private function getTemporaryTableCommentsQuery($view = null, $action = null, $last_timestamp = null) {
216
        if ($view == "comments_game_comments") {
217
            $where_clause = "WHERE temp.comment_type = 'game_comment'";
218
        } elseif ($view == "comments_game_review_comments") {
219
            $where_clause = "WHERE temp.comment_type = 'game_review_comment'";
220
        } elseif ($view == "comments_interview_comments") {
221
            $where_clause = "WHERE temp.comment_type = 'interview_comment'";
222
        } elseif ($view == "comments_article_comments") {
223
            $where_clause = "WHERE temp.comment_type = 'article_comment'";
224
        } else {
225
            $where_clause = "";
226
        }
227
        if (isset($action) and $action=="autoload") {
228
            if (strlen($where_clause) >1) {
229
                $where_clause .= " AND temp.timestamp < $last_timestamp ";
230
            } elseif (strlen($where_clause) == 0) {
231
                $where_clause .= " WHERE temp.timestamp < $last_timestamp ";
232
            }
233
        }
234
        
235
        if (isset($action) and $action=="search") {
236
            $where_clause .= " WHERE temp.timestamp <= $last_timestamp ";
237
        }
238
239
        $query = "SELECT
240
            temp.comments_id,
241
            temp.timestamp,
242
            temp.user_id,
243
            temp.user_comment_count,
244
            temp.user_subm_count,
245
            temp.userid,
246
            temp.email,
247
            temp.join_date,
248
            temp.karma,
249
            temp.show_email,
250
            temp.avatar_ext,
251
            temp.game_id,
252
            temp.game_name,
253
            temp.comment_type,
254
            comments.comment,
255
            temp.review_id
256
        FROM temp
257
        LEFT JOIN comments
258
            ON (temp.comments_id = comments.comments_id)
259
        ". $where_clause ."
260
        ORDER BY temp.timestamp DESC LIMIT 5";
261
262
        return $query;
263
    }
264
265
    /**
266
     * Get list of comments
267
     *
268
     * @param  integer $user_id Optional ID of a user
269
     * @param  string $view Optional string to id what kind of list of comments to be rendered
270
     * @param  string $action Optional string to id scroll autoload
271
     * @param  integer $last_timestamp timestamp used for the autoload
272
     * @return integer Number of comments
273
     */
274
    public function getCommentsBuild($view = null, $user_id = null, $action = null, $last_timestamp = null) {
275
276
        //Create temporary table
277
        $stmt = \AL\Db\execute_query(
0 ignored issues
show
Unused Code introduced by
The assignment to $stmt is dead and can be removed.
Loading history...
278
            "CommentsDAO: Create temporary table",
279
            $this->mysqli,
280
            $this->createTemporaryTableCommentsQuery(),
281
            null,
282
            null
283
        );
284
285
        //Populate temporary TABLE
286
        if (isset($view) and $view == "users_comments") {
287
            if (isset($user_id)) {
288
                $sql_build_game = $this->getGameCommentQuery($user_id);
289
                $sql_build_gamereview = $this->getGameReviewCommentQuery($user_id);
290
                $sql_build_interview = $this->getInterviewCommentQuery($user_id);
291
                $sql_build_article = $this->getArticleCommentQuery($user_id);
292
            }
293
        } else {
294
            $sql_build_game = $this->getGameCommentQuery();
295
            $sql_build_gamereview = $this->getGameReviewCommentQuery();
296
            $sql_build_interview = $this->getInterviewCommentQuery();
297
            $sql_build_article = $this->getArticleCommentQuery();
298
        }
299
        // Insert game comments
300
        $stmt = \AL\Db\execute_query(
301
            "CommentsDAO: Insert game comments into temp table",
302
            $this->mysqli,
303
            "INSERT INTO temp $sql_build_game",
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sql_build_game does not seem to be defined for all execution paths leading up to this point.
Loading history...
304
            null,
305
            null
306
        );
307
        // Insert game review comments
308
        $stmt = \AL\Db\execute_query(
309
            "CommentsDAO: Insert game review comments into temp table",
310
            $this->mysqli,
311
            "INSERT INTO temp $sql_build_gamereview",
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sql_build_gamereview does not seem to be defined for all execution paths leading up to this point.
Loading history...
312
            null,
313
            null
314
        );
315
        // Insert Interview comments
316
        $stmt = \AL\Db\execute_query(
317
            "CommentsDAO: Insert interview comments into temp table",
318
            $this->mysqli,
319
            "INSERT INTO temp $sql_build_interview",
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sql_build_interview does not seem to be defined for all execution paths leading up to this point.
Loading history...
320
            null,
321
            null
322
        );
323
        // Insert Article comments
324
        $stmt = \AL\Db\execute_query(
325
            "CommentsDAO: Insert article comments into temp table",
326
            $this->mysqli,
327
            "INSERT INTO temp $sql_build_article",
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sql_build_article does not seem to be defined for all execution paths leading up to this point.
Loading history...
328
            null,
329
            null
330
        );
331
332
        // Query temporary table
333
        $stmt = \AL\Db\execute_query(
334
            "CommentsDAO: Query temporary comments table",
335
            $this->mysqli,
336
            $this->getTemporaryTableCommentsQuery($view, $action, $last_timestamp),
337
            null,
338
            null
339
        );
340
341
        \AL\Db\bind_result(
342
            "CommentsDAO: Get comments",
343
            $stmt,
344
            $comments_id,
345
            $timestamp,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $timestamp does not exist. Did you maybe mean $last_timestamp?
Loading history...
346
            $user_id,
347
            $user_comment_count,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user_comment_count seems to be never defined.
Loading history...
348
            $user_subm_count,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user_subm_count seems to be never defined.
Loading history...
349
            $userid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userid does not exist. Did you maybe mean $user_id?
Loading history...
350
            $email,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $email seems to be never defined.
Loading history...
351
            $join_date,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $join_date seems to be never defined.
Loading history...
352
            $karma,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $karma seems to be never defined.
Loading history...
353
            $show_email,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $show_email seems to be never defined.
Loading history...
354
            $avatar_ext,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $avatar_ext seems to be never defined.
Loading history...
355
            $game_id,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $game_id seems to be never defined.
Loading history...
356
            $game_name,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $game_name seems to be never defined.
Loading history...
357
            $comment_type,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $comment_type does not exist. Did you maybe mean $comments?
Loading history...
358
            $comment,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $comment does not exist. Did you maybe mean $comments?
Loading history...
359
            $review_id
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $review_id seems to be never defined.
Loading history...
360
        );
361
362
        $comments = [];
363
        while ($stmt->fetch()) {
364
            $comments[] = new \AL\Common\Model\Comments\Comments(
365
                $comments_id,
366
                $timestamp,
367
                $user_id,
368
                $user_comment_count,
369
                $user_subm_count,
370
                $userid,
371
                $email,
372
                $join_date,
373
                $karma,
374
                $show_email,
375
                $avatar_ext,
376
                $game_id,
377
                $game_name,
378
                $comment_type,
379
                $comment,
380
                $review_id
381
            );
382
        }
383
384
        $stmt->close();
385
386
        return $comments;
387
    }
388
389
    /**
390
     * Get the total count of comments on the website
391
     *
392
     * @param  integer $user_id Optional ID of a user to count comments for
393
     * @return integer Number of comments
394
     */
395
    public function getCommentCount($user_id = null) {
396
        if (isset($user_id)) {
397
            $stmt = \AL\Db\execute_query(
398
                "CommentsDAO: Get comment count for user_id $user_id",
399
                $this->mysqli,
400
                "SELECT COUNT(*) FROM comments WHERE user_id = ?",
401
                "i",
402
                $user_id
403
            );
404
        } else {
405
            $stmt = \AL\Db\execute_query(
406
                "CommentsDAO: Get number of comments in database",
407
                $this->mysqli,
408
                "SELECT COUNT(*) FROM comments",
409
                null,
410
                null
411
            );
412
        }
413
414
        \AL\Db\bind_result(
415
            "CommentsDAO: Get comments count",
416
            $stmt,
417
            $count
418
        );
419
420
        $stmt->fetch();
421
        $stmt->close();
422
423
        return $count;
424
    }
425
426
    /**
427
     * Get the comment text for a specific comment
428
     *
429
     * @param  integer $comments_id ID of a comment
430
     * @return text the text of the comment
0 ignored issues
show
Bug introduced by
The type AL\Common\DAO\text 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...
431
     */
432
    public function getCommentText($comments_id = null, $action = null) {
433
        if (isset($comments_id)) {
434
            $stmt = \AL\Db\execute_query(
435
                "CommentsDAO: Get comment text for comments_id $comments_id",
436
                $this->mysqli,
437
                "SELECT comment FROM comments WHERE comments_id = ?",
438
                "i",
439
                $comments_id
440
            );
441
        }
442
443
        \AL\Db\bind_result(
444
            "CommentsDAO: Get comment text",
445
            $stmt,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $stmt does not seem to be defined for all execution paths leading up to this point.
Loading history...
446
            $comment
447
        );
448
449
        $stmt->fetch();
450
        
451
        $oldcomment = $comment;
452
        $oldcomment = nl2br($oldcomment);
453
        $oldcomment = InsertALCode($oldcomment);
454
        $oldcomment = trim($oldcomment);
455
        $oldcomment = RemoveSmillies($oldcomment);
456
        $comment = stripslashes($oldcomment);
457
        if (isset($action) and $action == 'get_comment_text') {
458
            $breaks = array("<br />","<br>","<br/>");
459
            $comment = str_ireplace($breaks, "\r\n", $comment);
460
        }
461
        
462
        $stmt->close();
463
464
        return $comment;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $comment returns the type array|string which is incompatible with the documented return type AL\Common\DAO\text.
Loading history...
465
    }
466
467
    /**
468
     * Update the comment text for a specific comment
469
     *
470
     * @param  integer $comments_id ID of a comment
471
     * @param  text $comments_text the text of the comment
472
     * @return text the text of the comment
473
     */
474
    public function saveCommentText($comments_id, $comment_text, $comment_type) {
475
        if (isset($comments_id)) {
476
            $stmt = \AL\Db\execute_query(
477
                "CommentsDAO: Save comment text for comments_id $comments_id",
478
                $this->mysqli,
479
                "UPDATE comments SET comment = ? WHERE comments_id = ?",
480
                "si",
481
                $comment_text,
482
                $comments_id
483
            );
484
485
            if ($comment_type ==  "game_comment") {
486
                create_log_entry('Games', $comments_id, 'Comment', $comments_id, 'Update', $_SESSION['user_id']);
487
            } elseif ($comment_type ==  "game_review_comment") {
488
                create_log_entry('Reviews', $comments_id, 'Comment', $comments_id, 'Update', $_SESSION['user_id']);
489
            } elseif ($comment_type ==  "interview_comment") {
490
                create_log_entry('Interviews', $comments_id, 'Comment', $comments_id, 'Update', $_SESSION['user_id']);
491
            } elseif ($comment_type ==  "article_comment") {
492
                create_log_entry('Articles', $comments_id, 'Comment', $comments_id, 'Update', $_SESSION['user_id']);
493
            }
494
        }
495
496
        $stmt->fetch();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $stmt does not seem to be defined for all execution paths leading up to this point.
Loading history...
497
        $stmt->close();
498
499
        return;
500
    }
501
}
502