Passed
Push — Showing-Posts ( 95ba49...a053fc )
by Stone
02:03
created

PostModel   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 331
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 116
dl 0
loc 331
rs 10
c 0
b 0
f 0
wmc 18

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getPosts() 0 3 1
A newPost() 0 27 1
A addExcerpt() 0 9 2
A getPostsWithAuthor() 0 14 1
A getAllPublishedPosts() 0 15 2
A getPostsInCategory() 0 14 1
A totalNumberPosts() 0 6 1
A modifyPost() 0 35 1
A totalNumberPostsByTag() 0 7 1
A getSinglePost() 0 9 1
A totalNumberPostsInCategory() 0 7 1
A getPostsWithTag() 0 18 1
A getFrontPosts() 0 3 1
A totalNumberPostsByAuthor() 0 7 1
A basePostSelect() 0 6 1
A __construct() 0 7 1
1
<?php
2
3
namespace App\Models;
4
5
use Core\Constant;
6
use Core\Container;
7
use Core\Model;
8
use Core\Traits\StringFunctions;
9
10
class PostModel extends Model
11
{
12
    use StringFunctions;
13
14
    private $postsTbl;
15
    private $categoriesTbl;
16
    private $usersTbl;
17
    private $postTagTbl;
18
19
    public function __construct(Container $container)
20
    {
21
        parent::__construct($container);
22
        $this->postsTbl = $this->getTablePrefix("posts");
23
        $this->categoriesTbl = $this->getTablePrefix("categories");
24
        $this->usersTbl = $this->getTablePrefix("users");
25
        $this->postTagTbl = $this->getTablePrefix("posts_has_tags");
26
    }
27
28
    /**
29
     * the base Select SQL to get the information from the post table and joined tables
30
     * @return string
31
     */
32
    private function basePostSelect(): string
33
    {
34
        $sql = "SELECT idposts, title, post_image,article,$this->postsTbl.last_update, posts_slug, categories_idcategories, category_name, published, on_front_page, categories_slug, pseudo as author, idusers
35
                FROM $this->postsTbl INNER JOIN $this->categoriesTbl ON $this->postsTbl.categories_idcategories = $this->categoriesTbl.idcategories
36
                INNER JOIN $this->usersTbl ON $this->postsTbl.author_iduser = $this->usersTbl.idusers";
37
        return $sql;
38
    }
39
40
    /**
41
     * add the excerpt to a post list
42
     * @param array $posts
43
     * @return array
44
     * @throws \ErrorException
45
     */
46
    private function addExcerpt(array $posts): array
47
    {
48
        $sendResults = [];
49
        //we create the excerpt for the text and add it to the object
50
        foreach ($posts as $post) {
51
            $post->{'excerpt'} = $this->getExcerpt($post->article);
52
            $sendResults[] = $post;
53
        }
54
        return $sendResults;
55
    }
56
57
    /**
58
     * get all the posts with details. Only selecting posts that are published
59
     * @param int $offset where to start (for pagination)
60
     * @param int $limit the number of posts
61
     * @param bool $isFrontPage extract only front page posts
62
     * @return array list of posts
63
     * @throws \ErrorException
64
     */
65
    private function getAllPublishedPosts(int $offset, int $limit, bool $isFrontPage = false): array
66
    {
67
        $sql = $this->basePostSelect();
68
        $sql .= " WHERE published = 1";
69
        if ($isFrontPage) {
70
            $sql .= " AND on_front_page = 1";
71
        }
72
        $sql .= " ORDER BY $this->postsTbl.creation_date DESC";
73
        $sql .= " LIMIT :limit OFFSET :offset";
74
        $this->query($sql);
75
        $this->bind(":limit", $limit);
76
        $this->bind(":offset", $offset);
77
        $this->execute();
78
        $results = $this->fetchAll();
79
        return $this->addExcerpt($results);
80
    }
81
82
    /**
83
     * get the total number of posts
84
     * @return int
85
     * @throws \Exception
86
     */
87
    public function totalNumberPosts(): int
88
    {
89
        $sql = "SELECT COUNT(*) FROM $this->postsTbl WHERE published = 1";
90
        $this->query($sql);
91
        $this->execute();
92
        return $this->stmt->fetchColumn();
93
    }
94
95
    /**
96
     * get the total number of posts in a category
97
     * @param int $categoryId
98
     * @return int
99
     * @throws \Exception
100
     */
101
    public function totalNumberPostsInCategory(int $categoryId): int
102
    {
103
        $sql = "SELECT COUNT(*) FROM $this->postsTbl WHERE published = 1 AND categories_idcategories = :categoryId ";
104
        $this->query($sql);
105
        $this->bind(":categoryId", $categoryId, \PDO::PARAM_INT);
106
        $this->execute();
107
        return $this->stmt->fetchColumn();
108
    }
109
110
    /**
111
     * get the total number of posts in a category
112
     * @param int $authorid
113
     * @return int
114
     * @throws \Exception
115
     */
116
    public function totalNumberPostsByAuthor(int $authorid):int
117
    {
118
        $sql = "SELECT COUNT(*) FROM $this->postsTbl WHERE published = 1 AND author_iduser = :authorId ";
119
        $this->query($sql);
120
        $this->bind(":authorId", $authorid, \PDO::PARAM_INT);
121
        $this->execute();
122
        return $this->stmt->fetchColumn();
123
    }
124
125
    /**
126
     * get number of posts with tag
127
     * @param int $tagId
128
     * @return int
129
     * @throws \Exception
130
     */
131
    public function totalNumberPostsByTag(int $tagId):int
132
    {
133
        $sql = "SELECT COUNT(*) FROM $this->postTagTbl WHERE tag_idtags = :tagId ";
134
        $this->query($sql);
135
        $this->bind(":tagId", $tagId, \PDO::PARAM_INT);
136
        $this->execute();
137
        return $this->stmt->fetchColumn();
138
    }
139
140
141
    /**
142
     * get the list of front posts
143
     * @param int $offset
144
     * @param int $limit
145
     * @return array
146
     * @throws \ErrorException
147
     */
148
    public function getFrontPosts(int $offset = 0, int $limit = Constant::FRONT_PAGE_POSTS): array
149
    {
150
        return $this->getAllPublishedPosts($offset, $limit, true);
151
    }
152
153
    /**
154
     * get the list of all the posts.
155
     * @param int $offset
156
     * @param int $limit
157
     * @return array
158
     * @throws \ErrorException
159
     */
160
    public function getPosts(int $offset = 0, int $limit = Constant::POSTS_PER_PAGE): array
161
    {
162
        return $this->getAllPublishedPosts($offset, $limit, false);
163
    }
164
165
    /**
166
     * get all the posts from a certain category
167
     * @param int $categoryId the id of the category
168
     * @param int $offset the offset for pagination
169
     * @param int $limit the limit to display
170
     * @return array list of posts in set category
171
     * @throws \Exception
172
     */
173
    public function getPostsInCategory(int $categoryId, int $offset = 0, int $limit = Constant::POSTS_PER_PAGE): array
174
    {
175
        $sql = $this->basePostSelect();
176
        $sql .= " WHERE categories_idcategories = :categoryId 
177
                ORDER BY $this->postsTbl.creation_date DESC
178
                LIMIT :limit OFFSET :offset;";
179
        $this->query($sql);
180
        $this->bind(":categoryId", $categoryId, \PDO::PARAM_INT);
181
        $this->bind(":limit", $limit);
182
        $this->bind(":offset", $offset);
183
        $this->execute();
184
185
        $results = $this->fetchAll();
186
        return $this->addExcerpt($results);
187
    }
188
189
    /**
190
     * get all the posts with a specific author
191
     * @param int $authorId
192
     * @param int $offset
193
     * @param int $limit
194
     * @return array
195
     * @throws \ErrorException
196
     */
197
    public function getPostsWithAuthor(int $authorId, int $offset = 0, int $limit = Constant::POSTS_PER_PAGE): array
198
    {
199
        $sql = $this->basePostSelect();
200
        $sql .= " WHERE author_iduser = :authorId 
201
                ORDER BY $this->postsTbl.creation_date DESC
202
                LIMIT :limit OFFSET :offset;";
203
        $this->query($sql);
204
        $this->bind(":authorId", $authorId, \PDO::PARAM_INT);
205
        $this->bind(":limit", $limit);
206
        $this->bind(":offset", $offset);
207
        $this->execute();
208
209
        $results = $this->fetchAll();
210
        return $this->addExcerpt($results);
211
    }
212
213
214
    public function getPostsWithTag(int $tagId, int $offset = 0, int $limit = Constant::POSTS_PER_PAGE): array
215
    {
216
        $sql = "SELECT idposts, title, post_image,article,$this->postsTbl.last_update, posts_slug, categories_idcategories, category_name, published, on_front_page, categories_slug, pseudo as author, idusers
217
                FROM $this->postsTbl INNER JOIN $this->categoriesTbl ON $this->postsTbl.categories_idcategories = $this->categoriesTbl.idcategories
218
                INNER JOIN $this->usersTbl ON $this->postsTbl.author_iduser = $this->usersTbl.idusers
219
                LEFT JOIN $this->postTagTbl ON $this->postsTbl.idposts = $this->postTagTbl.post_idposts
220
                WHERE tag_idtags = :tagId
221
                ORDER BY $this->postsTbl.creation_date DESC
222
                LIMIT :limit OFFSET :offset;";
223
224
        $this->query($sql);
225
        $this->bind(":tagId", $tagId, \PDO::PARAM_INT);
226
        $this->bind(":limit", $limit);
227
        $this->bind(":offset", $offset);
228
        $this->execute();
229
230
        $results = $this->fetchAll();
231
        return $this->addExcerpt($results);
232
    }
233
234
    /**
235
     * get a single post from it's ID
236
     * @param int $postid the post ID to get
237
     * @return array the single post details
238
     * @throws \Exception
239
     */
240
    public function getSinglePost(int $postid)
241
    {
242
        $sql = $this->basePostSelect();
243
        $sql .= " WHERE idposts = :postId;";
244
        $this->query($sql);
245
        $this->bind(":postId", $postid, \PDO::PARAM_INT);
246
        $this->execute();
247
248
        return $this->fetch();
249
    }
250
251
    /**
252
     * Create a new post
253
     * @param string $title
254
     * @param string $postImage
255
     * @param int $idCategory
256
     * @param string $article
257
     * @param int $idUser
258
     * @param int $published
259
     * @param int $onFrontPage
260
     * @param string $postSlug
261
     * @return int the id of created post
262
     * @throws \Exception
263
     */
264
    public function newPost(
265
        string $title,
266
        string $postImage,
267
        int $idCategory,
268
        string $article,
269
        int $idUser,
270
        int $published,
271
        int $onFrontPage,
272
        string $postSlug
273
    ): int {
274
        $sql = "
275
          INSERT INTO $this->postsTbl (title, post_image, categories_idcategories, article, author_iduser, creation_date, last_update, published, on_front_page, posts_slug)
276
          VALUES (:title, :post_image, :categories_idcategories, :article, :author_iduser, NOW(), NOW(), :published, :on_front_page, :posts_slug)
277
        ";
278
        $this->query($sql);
279
        $this->bind(':title', $title);
280
        $this->bind(':post_image', $postImage);
281
        $this->bind(':categories_idcategories', $idCategory);
282
        $this->bind(':article', $article);
283
        $this->bind(':author_iduser', $idUser);
284
        $this->bind(':published', $published);
285
        $this->bind(':on_front_page', $onFrontPage);
286
        $this->bind(':posts_slug', $postSlug);
287
288
        $this->execute();
289
290
        return $this->dbh->lastInsertId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dbh->lastInsertId() returns the type string which is incompatible with the type-hinted return integer.
Loading history...
291
    }
292
293
    /**
294
     * Update a post with new values
295
     * @param int $postId
296
     * @param string $title
297
     * @param string $postImage
298
     * @param int $idCategory
299
     * @param string $article
300
     * @param int $published
301
     * @param int $onFrontPage
302
     * @param string $postSlug
303
     * @return bool success
304
     * @throws \Exception
305
     */
306
    public function modifyPost(
307
        int $postId,
308
        string $title,
309
        string $postImage,
310
        int $idCategory,
311
        string $article,
312
        int $published,
313
        int $onFrontPage,
314
        string $postSlug
315
    ): bool {
316
        $sql = "
317
            UPDATE $this->postsTbl 
318
            SET 
319
                title = :title,
320
                post_image = :postImage,
321
                categories_idcategories = :idCategory,
322
                article = :article,
323
                last_update = NOW(),
324
                published = :published,
325
                on_front_page = :onFrontPage,
326
                posts_slug = :postSlug
327
            WHERE
328
              idposts = :postId
329
        ;";
330
        $this->query($sql);
331
        $this->bind(":title", $title);
332
        $this->bind(":postImage", $postImage);
333
        $this->bind(":idCategory", $idCategory);
334
        $this->bind(":article", $article);
335
        $this->bind(":published", $published);
336
        $this->bind(":onFrontPage", $onFrontPage);
337
        $this->bind(":postSlug", $postSlug);
338
        $this->bind(":postId", $postId);
339
340
        return $this->execute();
341
    }
342
343
344
}