Passed
Push — Comments ( 0e09d9...73e073 )
by Stone
02:13
created

CommentModel::addComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 3
dl 0
loc 13
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
        return $this->execute();
94
    }
95
96
    /**
97
     * Add a comment to a post
98
     * @param int $postId
99
     * @param int $userId
100
     * @param string $message
101
     * @return int
102
     * @throws \Exception
103
     */
104
    public function addComment(int $postId, int $userId, string $message):int
105
    {
106
        $sql="
107
            INSERT INTO $this->commentTbl (users_idusers, posts_idposts, comment, approved)
108
            VALUES (:userId, :postId, :comment, 0)
109
        ";
110
        $this->query($sql);
111
        $this->bind(':userId', $userId);
112
        $this->bind(':postId', $postId);
113
        $this->bind(':comment', $message);
114
115
        $this->execute();
116
        return (int)$this->dbh->lastInsertId();
117
    }
118
119
}