GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Comment::countComments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
/**
3
 * Comment Class
4
 *
5
 * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
6
 * @author     Omar El Gabry <[email protected]>
7
 */
8
9
class Comment extends Model{
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
11
    /**
12
     * get all comments of a post
13
     *
14
     * @access public
15
     * @param  array     $postId
16
     * @param  integer   $pageNum
17
     * @param  integer   $commentsCreated
18
     * @return array    Associative array of the comments, and Pagination Object(View More).
19
     *
20
     */
21
    public function getAll($postId, $pageNum = 1, $commentsCreated = 0){
22
23
        // Only for comments, We use $commentsCreated
24
        // What's it? Whenever we create a comment, It will be added in-place to the current comments in current .php page,
25
        // So, we need to track of those were created, and skip them in the Pagination($offset & $totalCount).
26
27
        $options    = "WHERE comments.post_id = :post_id ";
28
        $pagination = Pagination::pagination("comments", $options, [":post_id" => $postId], $pageNum, $commentsCreated);
29
        $offset     = $pagination->getOffset() + $commentsCreated;
30
        $limit      = $pagination->perPage;
31
32
        $database   = Database::openConnection();
33
        $query  = "SELECT comments.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, comments.content, comments.date ";
34
        $query .= "FROM users, posts, comments ";
35
        $query .= "WHERE comments.post_id = :post_id ";
36
        $query .= "AND posts.id = comments.post_id ";
37
        $query .= "AND users.id = comments.user_id ";
38
        $query .= "ORDER BY comments.date DESC ";
39
        $query .= "LIMIT $limit OFFSET $offset";
40
41
        $database->prepare($query);
42
        $database->bindValue(':post_id', (int)$postId);
43
        $database->execute();
44
        $comments = $database->fetchAllAssociative();
45
46
        // you can have post with no comments yet!
47
        return array("comments" => $comments, "pagination" => $pagination);
48
    }
49
50
    /**
51
     * get comment by Id
52
     *
53
     * @access public
54
     * @param  string   $commentId
55
     * @return array    Array holds the data of the comment
56
     *
57
     */
58 View Code Duplication
    public function getById($commentId){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
60
        $database = Database::openConnection();
61
        $query  = "SELECT comments.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, comments.content, comments.date ";
62
        $query .= "FROM users, posts, comments ";
63
        $query .= "WHERE comments.id = :id ";
64
        $query .= "AND posts.id = comments.post_id ";
65
        $query .= "AND users.id = comments.user_id LIMIT 1";
66
67
        $database->prepare($query);
68
        $database->bindValue(':id', (int)$commentId);
69
        $database->execute();
70
71
        $comment = $database->fetchAllAssociative();
72
        return $comment;
73
    }
74
75
    /**
76
     * create Comment.
77
     *
78
     * @access public
79
     * @param  string   $userId
80
     * @param  string   $postId
81
     * @param  string   $content
82
     * @return array    Array holds the created comment
83
     * @throws Exception If comment couldn't be created
84
     *
85
     */
86
    public function create($userId, $postId, $content){
87
88
        $validation = new Validation();
89
        if(!$validation->validate([ 'Content' => [$content, 'required|minLen(1)|maxLen(300)']])) {
90
            $this->errors = $validation->errors();
91
            return false;
92
        }
93
94
        $database = Database::openConnection();
95
        $query = "INSERT INTO comments (user_id, post_id, content) VALUES (:user_id, :post_id, :content)";
96
97
        $database->prepare($query);
98
        $database->bindValue(':user_id', $userId);
99
        $database->bindValue(':post_id', $postId);
100
        $database->bindValue(':content', $content);
101
        $database->execute();
102
103
        if($database->countRows() !== 1){
104
            throw new Exception ("Couldn't add comment");
105
        }
106
107
        $commentId = $database->lastInsertedId();
108
        $comment = $this->getById($commentId);
109
        return $comment;
110
    }
111
112
    /**
113
     * update Comment
114
     *
115
     * @access public
116
     * @param  string   $commentId
117
     * @param  string   $content
118
     * @return array    Array holds the updated comment
119
     * @throws Exception If comment couldn't be updated
120
     *
121
     */
122 View Code Duplication
    public function update($commentId, $content){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
124
        $validation = new Validation();
125
        if(!$validation->validate([ 'Content' => [$content, 'required|minLen(1)|maxLen(300)']])) {
126
            $this->errors = $validation->errors();
127
            return false;
128
        }
129
130
        $database = Database::openConnection();
131
        $query = "UPDATE comments SET content = :content WHERE id = :id LIMIT 1 ";
132
        $database->prepare($query);
133
        $database->bindValue(':content', $content);
134
        $database->bindValue(':id', $commentId);
135
        $result = $database->execute();
136
137
        if(!$result){
138
            throw new Exception("Couldn't update comment of ID: " . $commentId);
139
        }
140
141
        $comment = $this->getById($commentId);
142
        return $comment;
143
    }
144
145
     /**
146
      * counting the number of comments of a post.
147
      *
148
      * @access public
149
      * @static static  method
150
      * @param  string  $postId
151
      * @return integer number of comments
152
      *
153
      */
154 View Code Duplication
    public static function countComments($postId){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
156
        $database = Database::openConnection();
157
        $database->prepare("SELECT COUNT(*) AS count FROM comments WHERE post_id = :post_id");
158
        $database->bindValue(":post_id", $postId);
159
        $database->execute();
160
161
        return (int)$database->fetchAssociative()["count"];
162
    }
163
164
}