Passed
Push — Showing-Posts ( 329372...95ba49 )
by Stone
01:58
created

PostModel::totalNumberPostsByAuthor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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
18
    public function __construct(Container $container)
19
    {
20
        parent::__construct($container);
21
        $this->postsTbl = $this->getTablePrefix('posts');
22
        $this->categoriesTbl = $this->getTablePrefix('categories');
23
        $this->usersTbl = $this->getTablePrefix('users');
24
    }
25
26
    /**
27
     * the base Select SQL to get the information from the post table and joined tables
28
     * @return string
29
     */
30
    private function basePostSelect(): string
31
    {
32
        $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
33
                FROM $this->postsTbl INNER JOIN $this->categoriesTbl ON $this->postsTbl.categories_idcategories = $this->categoriesTbl.idcategories
34
                INNER JOIN $this->usersTbl ON $this->postsTbl.author_iduser = $this->usersTbl.idusers";
35
        return $sql;
36
    }
37
38
    /**
39
     * add the excerpt to a post list
40
     * @param array $posts
41
     * @return array
42
     * @throws \ErrorException
43
     */
44
    private function addExcerpt(array $posts): array
45
    {
46
        $sendResults = [];
47
        //we create the excerpt for the text and add it to the object
48
        foreach ($posts as $post) {
49
            $post->{'excerpt'} = $this->getExcerpt($post->article);
50
            $sendResults[] = $post;
51
        }
52
        return $sendResults;
53
    }
54
55
    /**
56
     * get all the posts with details. Only selecting posts that are published
57
     * @param int $offset where to start (for pagination)
58
     * @param int $limit the number of posts
59
     * @param bool $isFrontPage extract only front page posts
60
     * @return array list of posts
61
     * @throws \ErrorException
62
     */
63
    private function getAllPublishedPosts(int $offset, int $limit, bool $isFrontPage = false): array
64
    {
65
        $sql = $this->basePostSelect();
66
        $sql .= " WHERE published = 1";
67
        if ($isFrontPage) {
68
            $sql .= " AND on_front_page = 1";
69
        }
70
        $sql .= " ORDER BY $this->postsTbl.creation_date DESC";
71
        $sql .= " LIMIT :limit OFFSET :offset";
72
        $this->query($sql);
73
        $this->bind(":limit", $limit);
74
        $this->bind(":offset", $offset);
75
        $this->execute();
76
        $results = $this->fetchAll();
77
        return $this->addExcerpt($results);
78
    }
79
80
    /**
81
     * get the total number of posts
82
     * @return int
83
     * @throws \Exception
84
     */
85
    public function totalNumberPosts(): int
86
    {
87
        $sql = "SELECT COUNT(*) FROM $this->postsTbl WHERE published = 1";
88
        $this->query($sql);
89
        $this->execute();
90
        return $this->stmt->fetchColumn();
91
    }
92
93
    /**
94
     * get the total number of posts in a category
95
     * @param int $categoryId
96
     * @return int
97
     * @throws \Exception
98
     */
99
    public function totalNumberPostsInCategory(int $categoryId): int
100
    {
101
        $sql = "SELECT COUNT(*) FROM $this->postsTbl WHERE published = 1 AND categories_idcategories = :categoryId ";
102
        $this->query($sql);
103
        $this->bind(":categoryId", $categoryId, \PDO::PARAM_INT);
104
        $this->execute();
105
        return $this->stmt->fetchColumn();
106
    }
107
108
    /**
109
     * get the total number of posts in a category
110
     * @param int $authorid
111
     * @return int
112
     * @throws \Exception
113
     */
114
    public function totalNumberPostsByAuthor(int $authorid):int
115
    {
116
        $sql = "SELECT COUNT(*) FROM $this->postsTbl WHERE published = 1 AND author_iduser = :authorId ";
117
        $this->query($sql);
118
        $this->bind(":authorId", $authorid, \PDO::PARAM_INT);
119
        $this->execute();
120
        return $this->stmt->fetchColumn();
121
    }
122
123
    /**
124
     * get the list of front posts
125
     * @param int $offset
126
     * @param int $limit
127
     * @return array
128
     * @throws \ErrorException
129
     */
130
    public function getFrontPosts(int $offset = 0, int $limit = Constant::FRONT_PAGE_POSTS): array
131
    {
132
        return $this->getAllPublishedPosts($offset, $limit, true);
133
    }
134
135
    /**
136
     * get the list of all the posts.
137
     * @param int $offset
138
     * @param int $limit
139
     * @return array
140
     * @throws \ErrorException
141
     */
142
    public function getPosts(int $offset = 0, int $limit = Constant::POSTS_PER_PAGE): array
143
    {
144
        return $this->getAllPublishedPosts($offset, $limit, false);
145
    }
146
147
    /**
148
     * get all the posts from a certain category
149
     * @param int $categoryId the id of the category
150
     * @param int $offset the offset for pagination
151
     * @param int $limit the limit to display
152
     * @return array list of posts in set category
153
     * @throws \Exception
154
     */
155
    public function getPostsInCategory(int $categoryId, int $offset = 0, int $limit = Constant::POSTS_PER_PAGE): array
156
    {
157
        $sql = $this->basePostSelect();
158
        $sql .= " WHERE categories_idcategories = :categoryId 
159
                ORDER BY $this->postsTbl.creation_date DESC
160
                LIMIT :limit OFFSET :offset;";
161
        $this->query($sql);
162
        $this->bind(":categoryId", $categoryId, \PDO::PARAM_INT);
163
        $this->bind(":limit", $limit);
164
        $this->bind(":offset", $offset);
165
        $this->execute();
166
167
        $results = $this->fetchAll();
168
        return $this->addExcerpt($results);
169
    }
170
171
    /**
172
     * get all the posts with a specific author
173
     * @param int $authorId
174
     * @param int $offset
175
     * @param int $limit
176
     * @return array
177
     * @throws \ErrorException
178
     */
179
    public function getPostsWithAuthor(int $authorId, int $offset = 0, int $limit = Constant::POSTS_PER_PAGE): array
180
    {
181
        $sql = $this->basePostSelect();
182
        $sql .= " WHERE author_iduser = :authorId 
183
                ORDER BY $this->postsTbl.creation_date DESC
184
                LIMIT :limit OFFSET :offset;";
185
        $this->query($sql);
186
        $this->bind(":authorId", $authorId, \PDO::PARAM_INT);
187
        $this->bind(":limit", $limit);
188
        $this->bind(":offset", $offset);
189
        $this->execute();
190
191
        $results = $this->fetchAll();
192
        return $this->addExcerpt($results);
193
    }
194
195
    /**
196
     * get a single post from it's ID
197
     * @param int $postid the post ID to get
198
     * @return array the single post details
199
     * @throws \Exception
200
     */
201
    public function getSinglePost(int $postid)
202
    {
203
        $sql = $this->basePostSelect();
204
        $sql .= " WHERE idposts = :postId;";
205
        $this->query($sql);
206
        $this->bind(":postId", $postid, \PDO::PARAM_INT);
207
        $this->execute();
208
209
        return $this->fetch();
210
    }
211
212
    /**
213
     * Create a new post
214
     * @param string $title
215
     * @param string $postImage
216
     * @param int $idCategory
217
     * @param string $article
218
     * @param int $idUser
219
     * @param int $published
220
     * @param int $onFrontPage
221
     * @param string $postSlug
222
     * @return int the id of created post
223
     * @throws \Exception
224
     */
225
    public function newPost(
226
        string $title,
227
        string $postImage,
228
        int $idCategory,
229
        string $article,
230
        int $idUser,
231
        int $published,
232
        int $onFrontPage,
233
        string $postSlug
234
    ): int {
235
        $sql = "
236
          INSERT INTO $this->postsTbl (title, post_image, categories_idcategories, article, author_iduser, creation_date, last_update, published, on_front_page, posts_slug)
237
          VALUES (:title, :post_image, :categories_idcategories, :article, :author_iduser, NOW(), NOW(), :published, :on_front_page, :posts_slug)
238
        ";
239
        $this->query($sql);
240
        $this->bind(':title', $title);
241
        $this->bind(':post_image', $postImage);
242
        $this->bind(':categories_idcategories', $idCategory);
243
        $this->bind(':article', $article);
244
        $this->bind(':author_iduser', $idUser);
245
        $this->bind(':published', $published);
246
        $this->bind(':on_front_page', $onFrontPage);
247
        $this->bind(':posts_slug', $postSlug);
248
249
        $this->execute();
250
251
        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...
252
    }
253
254
    /**
255
     * Update a post with new values
256
     * @param int $postId
257
     * @param string $title
258
     * @param string $postImage
259
     * @param int $idCategory
260
     * @param string $article
261
     * @param int $published
262
     * @param int $onFrontPage
263
     * @param string $postSlug
264
     * @return bool success
265
     * @throws \Exception
266
     */
267
    public function modifyPost(
268
        int $postId,
269
        string $title,
270
        string $postImage,
271
        int $idCategory,
272
        string $article,
273
        int $published,
274
        int $onFrontPage,
275
        string $postSlug
276
    ): bool {
277
        $sql = "
278
            UPDATE $this->postsTbl 
279
            SET 
280
                title = :title,
281
                post_image = :postImage,
282
                categories_idcategories = :idCategory,
283
                article = :article,
284
                last_update = NOW(),
285
                published = :published,
286
                on_front_page = :onFrontPage,
287
                posts_slug = :postSlug
288
            WHERE
289
              idposts = :postId
290
        ;";
291
        $this->query($sql);
292
        $this->bind(":title", $title);
293
        $this->bind(":postImage", $postImage);
294
        $this->bind(":idCategory", $idCategory);
295
        $this->bind(":article", $article);
296
        $this->bind(":published", $published);
297
        $this->bind(":onFrontPage", $onFrontPage);
298
        $this->bind(":postSlug", $postSlug);
299
        $this->bind(":postId", $postId);
300
301
        return $this->execute();
302
    }
303
304
305
}