Passed
Push — Comments ( 804108...f56c48 )
by Stone
02:05
created

CommentModel::getCommentsList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Core\Model;
6
use Core\Container;
7
use Core\Constant;
8
9
class CommentModel extends Model{
10
11
    private $commentTbl;
12
    private $userTbl;
13
14
    public function __construct(Container $container)
15
    {
16
        parent::__construct($container);
17
18
        $this->commentTbl = $this->getTablePrefix("comments");
19
        $this->userTbl = $this->getTablePrefix("users");
20
    }
21
22
    /**
23
     * Count the number of comments on a post
24
     * @param int $postId
25
     * @return int
26
     * @throws \Exception
27
     */
28
    public function countCommentsOnPost(int $postId): int
29
    {
30
        $sql = "SELECT COUNT(*) FROM $this->commentTbl WHERE posts_idposts = :postId";
31
        $this->query($sql);
32
        $this->bind(":postId", $postId);
33
        $this->execute();
34
        return $this->stmt->fetchColumn();
35
    }
36
37
    /**
38
     * the list of comments on a post with limit and offset
39
     * @param int $postId
40
     * @param int $offset
41
     * @param int $limit
42
     * @return bool
43
     * @throws \Exception
44
     */
45
    public function getCommentsListOnPost(int $postId, int $offset = 0, int $limit = Constant::COMMENTS_PER_PAGE)
46
    {
47
        $sql = "
48
            SELECT idcomments, users_idusers, posts_idposts, comment, approved, username, avatar
49
            FROM $this->commentTbl LEFT JOIN $this->userTbl ON $this->commentTbl.users_idusers = $this->userTbl.idusers
50
            WHERE approved = 1
51
            AND posts_idposts = :postId
52
            LIMIT :limit OFFSET :offset
53
        ";
54
55
        $this->query($sql);
56
        $this->bind(":limit", $limit);
57
        $this->bind(":offset", $offset);
58
        $this->bind(":postId", $postId);
59
        $this->execute();
60
        return $this->fetchAll();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fetchAll() returns the type array which is incompatible with the documented return type boolean.
Loading history...
61
    }
62
63
    /**
64
     * count the number of pending comments
65
     * @return mixed
66
     * @throws \Exception
67
     */
68
    public function countPendingComments()
69
    {
70
        $sql = "SELECT COUNT(*) FROM $this->commentTbl WHERE approved = 0";
71
        $this->query($sql);
72
        $this->execute();
73
        return $this->stmt->fetchColumn();
74
    }
75
76
    /**
77
     * get the list of pending comments with limit and offset
78
     * @param int $offset
79
     * @param int $limit
80
     * @return bool
81
     * @throws \Exception
82
     */
83
    public function getPendingCommentsList(int $offset = 0, int $limit = Constant::COMMENTS_PER_PAGE)
84
    {
85
        $sql = "
86
          SELECT * FROM $this->commentTbl 
87
          WHERE approved = 0
88
          LIMIT :limit OFFSET :offset
89
        ";
90
        $this->query($sql);
91
        $this->bind(":limit", $limit);
92
        $this->bind(":offset", $offset);
93
        $this->execute();
94
95
        return $this->fetchAll();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fetchAll() returns the type array which is incompatible with the documented return type boolean.
Loading history...
96
    }
97
98
    public function countComments(): int
99
    {
100
        return $this->count($this->commentTbl);
101
    }
102
103
104
    public function getCommentsList(int $offset = 0, int $limit = Constant::POSTS_PER_PAGE)
105
    {
106
        return $this->list($offset, $limit, $this->commentTbl);
107
    }
108
109
    /**
110
     * Add a comment to a post
111
     * @param int $postId
112
     * @param int $userId
113
     * @param string $message
114
     * @return int
115
     * @throws \Exception
116
     */
117
    public function addComment(int $postId, int $userId, string $message):int
118
    {
119
        $sql="
120
            INSERT INTO $this->commentTbl (users_idusers, posts_idposts, comment, approved)
121
            VALUES (:userId, :postId, :comment, 0)
122
        ";
123
        $this->query($sql);
124
        $this->bind(':userId', $userId);
125
        $this->bind(':postId', $postId);
126
        $this->bind(':comment', $message);
127
128
        $this->execute();
129
        return (int)$this->dbh->lastInsertId();
130
    }
131
132
}