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.

NewsFeed::getById()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 15
loc 15
rs 9.4285
cc 1
eloc 11
nc 1
nop 1
1
<?php
2
3
 /**
4
  * NewsFeed Class
5
  *
6
  * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
7
  * @author     Omar El Gabry <[email protected]>
8
  */
9
class NewsFeed 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 news feed.
13
     *
14
     * @access public
15
     * @param  integer  $pageNum
16
     * @return array
17
     *
18
     */
19 View Code Duplication
    public function getAll($pageNum = 1){
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...
20
21
        $pagination = Pagination::pagination("newsfeed", "", [], $pageNum);
22
        $offset     = $pagination->getOffset();
23
        $limit      = $pagination->perPage;
24
25
        $database   = Database::openConnection();
26
        $query  = "SELECT newsfeed.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, newsfeed.content, newsfeed.date ";
27
        $query .= "FROM users, newsfeed ";
28
        $query .= "WHERE users.id = newsfeed.user_id ";
29
        $query .= "ORDER BY newsfeed.date DESC ";
30
        $query .= "LIMIT $limit OFFSET $offset";
31
32
        $database->prepare($query);
33
        $database->execute();
34
        $newsfeed = $database->fetchAllAssociative();
35
36
        return array("newsfeed" => $newsfeed, "pagination" => $pagination);
37
     }
38
39
    /**
40
     * get news feed by Id.
41
     *
42
     * @param  string  $newsfeedId
43
     * @return array
44
      */
45 View Code Duplication
    public function getById($newsfeedId){
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...
46
47
        $database = Database::openConnection();
48
        $query  = "SELECT newsfeed.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, newsfeed.content, newsfeed.date ";
49
        $query .= "FROM users, newsfeed ";
50
        $query .= "WHERE newsfeed.id = :id ";
51
        $query .= "AND users.id = newsfeed.user_id  LIMIT 1 ";
52
53
        $database->prepare($query);
54
        $database->bindValue(':id', (int)$newsfeedId);
55
        $database->execute();
56
57
        $feed = $database->fetchAllAssociative();
58
        return $feed;
59
     }
60
61
    /**
62
     * create news feed.
63
     *
64
     * @param  integer $userId
65
     * @param  string  $content
66
     * @return array feed created
67
     * @throws Exception if feed couldn't be created
68
     */
69 View Code Duplication
    public function create($userId, $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...
70
71
        $validation = new Validation();
72
        if(!$validation->validate(['Content'   => [$content, "required|minLen(4)|maxLen(300)"]])) {
73
            $this->errors = $validation->errors();
74
            return false;
75
        }
76
77
        $database = Database::openConnection();
78
        $query    = "INSERT INTO newsfeed (user_id, content) VALUES (:user_id, :content)";
79
80
        $database->prepare($query);
81
        $database->bindValue(':user_id', $userId);
82
        $database->bindValue(':content', $content);
83
        $database->execute();
84
85
        if($database->countRows() !== 1){
86
            throw new Exception("Couldn't add news feed");
87
        }
88
89
        $newsfeedId = $database->lastInsertedId();
90
        $feed = $this->getById($newsfeedId);
91
        return $feed;
92
     }
93
94
    /**
95
     * update news feed.
96
     *
97
     * @param  string  $newsfeedId
98
     * @param  string  $content
99
     * @return array   feed created
100
     * @throws Exception if feed couldn't be updated
101
     */
102 View Code Duplication
    public function update($newsfeedId, $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...
103
104
        $validation = new Validation();
105
        if(!$validation->validate(['Content'   => [$content, "required|minLen(4)|maxLen(300)"]])) {
106
            $this->errors = $validation->errors();
107
            return false;
108
        }
109
110
        $database = Database::openConnection();
111
        $query = "UPDATE newsfeed SET content = :content WHERE id = :id LIMIT 1";
112
113
        $database->prepare($query);
114
        $database->bindValue(':content', $content);
115
        $database->bindValue(':id', $newsfeedId);
116
        $result = $database->execute();
117
118
        if(!$result){
119
            throw new Exception("Couldn't update newsfeed of ID: " . $newsfeedId);
120
        }
121
122
        $feed = $this->getById($newsfeedId);
123
        return $feed;
124
     }
125
126
 }