Passed
Push — Comments ( 60864f )
by Stone
03:30
created

CommentModel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
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
13
    public function __construct(Container $container)
14
    {
15
        parent::__construct($container);
16
17
        $this->commentTbl = $this->getTablePrefix("comments");
18
    }
19
20
    /**
21
     * Count the number of comments on a post
22
     * @param int $postId
23
     * @return int
24
     * @throws \Exception
25
     */
26
    public function countCommentsOnPost(int $postId): int
27
    {
28
        $sql = "SELECT COUNT(*) FROM $this->commentTbl WHERE posts_idposts = :postId";
29
        $this->query($sql);
30
        $this->bind(":postId", $postId);
31
        $this->execute();
32
        return $this->stmt->fetchColumn();
33
    }
34
35
    /**
36
     * the list of comments on a post with limit and offset
37
     * @param int $postId
38
     * @param int $offset
39
     * @param int $limit
40
     * @return bool
41
     * @throws \Exception
42
     */
43
    public function getCommentsListOnPost(int $postId, int $offset = 0, int $limit = Constant::COMMENTS_PER_PAGE)
44
    {
45
        $sql = "
46
            SELECT * FROM $this->commentTbl
47
            WHERE approved = 1
48
            AND posts_idposts = :postId
49
            LIMIT :limit OFFSET :offset
50
        ";
51
52
        $this->query($sql);
53
        $this->bind(":limit", $limit);
54
        $this->bind(":offset", $offset);
55
        $this->bind(":postId", $postId);
56
        return $this->execute();
57
    }
58
59
    /**
60
     * count the number of pending comments
61
     * @return mixed
62
     * @throws \Exception
63
     */
64
    public function countPendingComments()
65
    {
66
        $sql = "SELECT COUNT(*) FROM $this->commentTbl WHERE approved = 0";
67
        $this->query($sql);
68
        $this->execute();
69
        return $this->stmt->fetchColumn();
70
    }
71
72
    /**
73
     * get the list of pending comments with limit and offset
74
     * @param int $offset
75
     * @param int $limit
76
     * @return bool
77
     * @throws \Exception
78
     */
79
    public function getPendingCommentsList(int $offset = 0, int $limit = Constant::COMMENTS_PER_PAGE)
80
    {
81
        $sql = "
82
          SELECT * FROM $this->commentTbl 
83
          WHERE approved = 0
84
          LIMIT :limit OFFSET :offset
85
        ";
86
        $this->query($sql);
87
        $this->bind(":limit", $limit);
88
        $this->bind(":offset", $offset);
89
        return $this->execute();
90
    }
91
92
}