Completed
Push — master ( 196a6a...314b46 )
by Stone
12s
created

CommentModel   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 83
dl 0
loc 259
rs 10
c 0
b 0
f 0
wmc 14

14 Methods

Rating   Name   Duplication   Size   Complexity  
A purifyHtml() 0 5 1
A baseSql() 0 9 1
A __construct() 0 7 1
A countCommentsOnPost() 0 7 1
A countPendingComments() 0 6 1
A getPendingCommentsList() 0 13 1
A getCommentsListOnPost() 0 15 1
A countComments() 0 3 1
A addComment() 0 15 1
A delete() 0 9 1
A getCommentsList() 0 12 1
A update() 0 18 1
A setApproved() 0 13 1
A getCommentById() 0 11 1
1
<?php
2
3
namespace App\Models;
4
5
use Core\Model;
6
use Core\Container;
7
use Core\Constant;
8
use HTMLPurifier;
9
use HTMLPurifier_Config;
10
11
class CommentModel extends Model{
12
13
    private $commentTbl;
14
    private $userTbl;
15
    private $postTbl;
16
17
    public function __construct(Container $container)
18
    {
19
        parent::__construct($container);
20
21
        $this->commentTbl = $this->getTablePrefix("comments");
22
        $this->userTbl = $this->getTablePrefix("users");
23
        $this->postTbl = $this->getTablePrefix("posts");
24
    }
25
26
    /**
27
     * the base Select SQl
28
     * @return string
29
     */
30
    private function baseSql():string
31
    {
32
        $sql = "
33
            SELECT idcomments, users_idusers, posts_idposts, comment, approved, comment_date, idposts, title, posts_slug, idusers, username, avatar
34
            FROM $this->commentTbl 
35
            LEFT JOIN $this->postTbl ON $this->commentTbl.posts_idposts = $this->postTbl.idposts
36
            LEFT JOIN $this->userTbl ON $this->commentTbl.users_idusers = $this->userTbl.idusers
37
        ";
38
        return $sql;
39
    }
40
41
    /**
42
     * secure the HTML thanks to HTML Purifier
43
     * @param $dirtyHtml
44
     * @return string
45
     */
46
    private function purifyHtml($dirtyHtml):string
47
    {
48
        $config = HTMLPurifier_Config::createDefault();
49
        $purifier = new HTMLPurifier($config);
50
        return $purifier->purify($dirtyHtml);
51
    }
52
53
    /**
54
     * Count the number of comments on a post
55
     * @param int $postId
56
     * @return int
57
     * @throws \Exception
58
     */
59
    public function countCommentsOnPost(int $postId): int
60
    {
61
        $sql = "SELECT COUNT(*) FROM $this->commentTbl WHERE posts_idposts = :postId";
62
        $this->query($sql);
63
        $this->bind(":postId", $postId);
64
        $this->execute();
65
        return $this->stmt->fetchColumn();
66
    }
67
68
    /**
69
     * the list of comments on a post with limit and offset
70
     * @param int $postId
71
     * @param int $offset
72
     * @param int $limit
73
     * @return array
74
     * @throws \Exception
75
     */
76
    public function getCommentsListOnPost(int $postId, int $offset = 0, int $limit = Constant::COMMENTS_PER_PAGE):array
77
    {
78
        $sql = $this->baseSql();
79
        $sql .= "
80
            WHERE approved = 1
81
            AND posts_idposts = :postId
82
            LIMIT :limit OFFSET :offset
83
        ";
84
85
        $this->query($sql);
86
        $this->bind(":limit", $limit);
87
        $this->bind(":offset", $offset);
88
        $this->bind(":postId", $postId);
89
        $this->execute();
90
        return $this->fetchAll();
91
    }
92
93
    /**
94
     * count the number of pending comments
95
     * @return mixed
96
     * @throws \Exception
97
     */
98
    public function countPendingComments():int
99
    {
100
        $sql = "SELECT COUNT(*) FROM $this->commentTbl WHERE approved = 0";
101
        $this->query($sql);
102
        $this->execute();
103
        return $this->stmt->fetchColumn();
104
    }
105
106
    /**
107
     * get the list of pending comments with limit and offset
108
     * @param int $offset
109
     * @param int $limit
110
     * @return array
111
     * @throws \Exception
112
     */
113
    public function getPendingCommentsList(int $offset = 0, int $limit = Constant::COMMENTS_PER_PAGE):array
114
    {
115
        $sql = $this->baseSql();
116
        $sql .= "
117
          WHERE approved = 0
118
          LIMIT :limit OFFSET :offset
119
        ";
120
        $this->query($sql);
121
        $this->bind(":limit", $limit);
122
        $this->bind(":offset", $offset);
123
        $this->execute();
124
125
        return $this->fetchAll();
126
    }
127
128
    /**
129
     * counts all the comments
130
     * @return int
131
     * @throws \Exception
132
     */
133
    public function countComments(): int
134
    {
135
        return $this->count($this->commentTbl);
136
    }
137
138
139
    /**
140
     * get the list of all the comments
141
     * @param int $offset
142
     * @param int $limit
143
     * @return array
144
     * @throws \Exception
145
     */
146
    public function getCommentsList(int $offset = 0, int $limit = Constant::POSTS_PER_PAGE):array
147
    {
148
        $sql = $this->baseSql();
149
        $sql .= "
150
          LIMIT :limit OFFSET :offset
151
        ";
152
        $this->query($sql);
153
        $this->bind(":limit", $limit);
154
        $this->bind(":offset", $offset);
155
        $this->execute();
156
157
        return $this->fetchAll();
158
    }
159
160
    /**
161
     * Add a comment to a post
162
     * @param int $postId
163
     * @param int $userId
164
     * @param string $comment
165
     * @param bool $admin
166
     * @return int
167
     * @throws \Exception
168
     */
169
    public function addComment(int $postId, int $userId, string $comment, bool $admin=false):int
170
    {
171
        $comment = $this->purifyHtml($comment);
172
        $sql="
173
            INSERT INTO $this->commentTbl (users_idusers, posts_idposts, comment, approved, comment_date)
174
            VALUES (:userId, :postId, :comment, :approved, NOW())
175
        ";
176
        $this->query($sql);
177
        $this->bind(':userId', $userId);
178
        $this->bind(':postId', $postId);
179
        $this->bind(':comment', $comment);
180
        $this->bind(':approved', $admin);
181
182
        $this->execute();
183
        return (int)$this->dbh->lastInsertId();
184
    }
185
186
    /**
187
     * delete a comment by it's ID
188
     * @param int $commentId
189
     * @return bool
190
     * @throws \Exception
191
     */
192
    public function delete(int $commentId):bool
193
    {
194
        $sql = "
195
        DELETE FROM $this->commentTbl 
196
        WHERE idcomments = :commentId
197
        ";
198
        $this->query($sql);
199
        $this->bind(":commentId", $commentId);
200
        return $this->finalExecute();
201
    }
202
203
    /**
204
     * Update an existing comment
205
     * @param int $commentId
206
     * @param string $comment
207
     * @param bool $approved
208
     * @return bool
209
     * @throws \Exception
210
     */
211
    public function update(int $commentId, string $comment, bool $approved):bool
212
    {
213
        $comment = $this->purifyHtml($comment);
214
215
        $sql="
216
            UPDATE $this->commentTbl 
217
            SET
218
              comment = :comment,
219
              approved = :state
220
            WHERE
221
              idcomments = :commentId
222
        ";
223
224
        $this->query($sql);
225
        $this->bind(":commentId", $commentId);
226
        $this->bind(":comment", $comment);
227
        $this->bind(":state", $approved);
228
        return $this->finalExecute();
229
    }
230
231
    /**
232
     * get a comment from it's ID
233
     * @param int $commentId
234
     * @return mixed
235
     * @throws \Exception
236
     */
237
    public function getCommentById(int $commentId)
238
    {
239
        $sql = $this->baseSql();
240
        $sql .= "
241
          WHERE idcomments = :commentId
242
        ";
243
        $this->query($sql);
244
        $this->bind(':commentId', $commentId);
245
        $this->execute();
246
247
        return $this->fetch();
248
    }
249
250
    /**
251
     * Set the approved state
252
     * @param bool $state
253
     * @param int $commentId
254
     * @return bool
255
     * @throws \Exception
256
     */
257
    public function setApproved(bool $state, int $commentId):bool
258
    {
259
        $sql = "
260
            UPDATE $this->commentTbl 
261
            SET
262
              approved = :state
263
            WHERE
264
              idcomments = :commentId
265
        ";
266
        $this->query($sql);
267
        $this->bind(":commentId", $commentId);
268
        $this->bind(":state", $state);
269
        return $this->finalExecute();
270
    }
271
272
273
274
}