Passed
Push — Comments ( b886a4...7c68ce )
by Stone
02:19
created

CommentModel::addComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 4
dl 0
loc 14
rs 9.9666
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
    private $postTbl;
14
15
    public function __construct(Container $container)
16
    {
17
        parent::__construct($container);
18
19
        $this->commentTbl = $this->getTablePrefix("comments");
20
        $this->userTbl = $this->getTablePrefix("users");
21
        $this->postTbl = $this->getTablePrefix("posts");
22
    }
23
24
    private function baseSql()
25
    {
26
        $sql = "
27
            SELECT idcomments, users_idusers, posts_idposts, comment, approved, idposts, title, posts_slug, idusers, username, avatar
28
            FROM $this->commentTbl 
29
            LEFT JOIN $this->postTbl ON $this->commentTbl.posts_idposts = $this->postTbl.idposts
30
            LEFT JOIN $this->userTbl ON $this->commentTbl.users_idusers = $this->userTbl.idusers
31
        ";
32
        return $sql;
33
    }
34
35
    /**
36
     * Count the number of comments on a post
37
     * @param int $postId
38
     * @return int
39
     * @throws \Exception
40
     */
41
    public function countCommentsOnPost(int $postId): int
42
    {
43
        $sql = "SELECT COUNT(*) FROM $this->commentTbl WHERE posts_idposts = :postId";
44
        $this->query($sql);
45
        $this->bind(":postId", $postId);
46
        $this->execute();
47
        return $this->stmt->fetchColumn();
48
    }
49
50
    /**
51
     * the list of comments on a post with limit and offset
52
     * @param int $postId
53
     * @param int $offset
54
     * @param int $limit
55
     * @return bool
56
     * @throws \Exception
57
     */
58
    public function getCommentsListOnPost(int $postId, int $offset = 0, int $limit = Constant::COMMENTS_PER_PAGE)
59
    {
60
        $sql = $this->baseSql();
61
        $sql .= "
62
            WHERE approved = 1
63
            AND posts_idposts = :postId
64
            LIMIT :limit OFFSET :offset
65
        ";
66
67
        $this->query($sql);
68
        $this->bind(":limit", $limit);
69
        $this->bind(":offset", $offset);
70
        $this->bind(":postId", $postId);
71
        $this->execute();
72
        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...
73
    }
74
75
    /**
76
     * count the number of pending comments
77
     * @return mixed
78
     * @throws \Exception
79
     */
80
    public function countPendingComments()
81
    {
82
        $sql = "SELECT COUNT(*) FROM $this->commentTbl WHERE approved = 0";
83
        $this->query($sql);
84
        $this->execute();
85
        return $this->stmt->fetchColumn();
86
    }
87
88
    /**
89
     * get the list of pending comments with limit and offset
90
     * @param int $offset
91
     * @param int $limit
92
     * @return bool
93
     * @throws \Exception
94
     */
95
    public function getPendingCommentsList(int $offset = 0, int $limit = Constant::COMMENTS_PER_PAGE)
96
    {
97
        $sql = $this->baseSql();
98
        $sql .= "
99
          WHERE approved = 0
100
          LIMIT :limit OFFSET :offset
101
        ";
102
        $this->query($sql);
103
        $this->bind(":limit", $limit);
104
        $this->bind(":offset", $offset);
105
        $this->execute();
106
107
        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...
108
    }
109
110
    public function countComments(): int
111
    {
112
        return $this->count($this->commentTbl);
113
    }
114
115
116
    public function getCommentsList(int $offset = 0, int $limit = Constant::POSTS_PER_PAGE)
117
    {
118
        $sql = $this->baseSql();
119
        $sql .= "
120
          LIMIT :limit OFFSET :offset
121
        ";
122
        $this->query($sql);
123
        $this->bind(":limit", $limit);
124
        $this->bind(":offset", $offset);
125
        $this->execute();
126
127
        return $this->fetchAll();
128
    }
129
130
    /**
131
     * Add a comment to a post
132
     * @param int $postId
133
     * @param int $userId
134
     * @param string $message
135
     * @param bool $admin
136
     * @return int
137
     * @throws \Exception
138
     */
139
    public function addComment(int $postId, int $userId, string $message, bool $admin=false):int
140
    {
141
        $sql="
142
            INSERT INTO $this->commentTbl (users_idusers, posts_idposts, comment, approved)
143
            VALUES (:userId, :postId, :comment, :approved)
144
        ";
145
        $this->query($sql);
146
        $this->bind(':userId', $userId);
147
        $this->bind(':postId', $postId);
148
        $this->bind(':comment', $message);
149
        $this->bind(':approved', $admin);
150
151
        $this->execute();
152
        return (int)$this->dbh->lastInsertId();
153
    }
154
155
    /**
156
     * delete a comment by it's ID
157
     * @param int $commentId
158
     * @return bool
159
     * @throws \Exception
160
     */
161
    public function delete(int $commentId)
162
    {
163
        $sql = "
164
        DELETE FROM $this->commentTbl 
165
        WHERE idcomments = :commentId
166
        ";
167
        $this->query($sql);
168
        $this->bind(":commentId", $commentId);
169
        return $this->execute();
170
    }
171
172
    /**
173
     * Update an existing comment
174
     * @param int $commentId
175
     * @param string $comment
176
     * @param bool $approved
177
     * @return bool
178
     * @throws \Exception
179
     */
180
    public function update(int $commentId, string $comment, bool $approved)
181
    {
182
183
        $sql="
184
            UPDATE $this->commentTbl 
185
            SET
186
              comment = :comment,
187
              approved = :state
188
            WHERE
189
              idcomments = :commentId
190
        ";
191
192
        $this->query($sql);
193
        $this->bind(":commentId", $commentId);
194
        $this->bind(":comment", $comment);
195
        $this->bind(":state", $approved);
196
        return $this->execute();
197
    }
198
199
    /**
200
     * get a comment from it's ID
201
     * @param int $commentId
202
     * @return mixed
203
     * @throws \Exception
204
     */
205
    public function getCommentById(int $commentId)
206
    {
207
        $sql = $this->baseSql();
208
        $sql .= "
209
          WHERE idcomments = :commentId
210
        ";
211
        $this->query($sql);
212
        $this->bind(':commentId', $commentId);
213
        $this->execute();
214
215
        return $this->fetch();
216
    }
217
218
    /**
219
     * Set the approved state
220
     * @param bool $state
221
     * @param int $commentId
222
     * @return bool
223
     * @throws \Exception
224
     */
225
    public function setApproved(bool $state, int $commentId):bool
226
    {
227
        $sql = "
228
            UPDATE $this->commentTbl 
229
            SET
230
              approved = :state
231
            WHERE
232
              idcomments = :commentId
233
        ";
234
        $this->query($sql);
235
        $this->bind(":commentId", $commentId);
236
        $this->bind(":state", $state);
237
        return $this->execute();
238
    }
239
240
241
242
}