Passed
Push — Showing-Posts ( 7ae013...329372 )
by Stone
02:31
created

PostModel::getPostsWithAuthor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 3
dl 0
loc 14
rs 9.9332
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 list of front posts
82
     * @param int $offset
83
     * @param int $limit
84
     * @return array
85
     * @throws \ErrorException
86
     */
87
    public function getFrontPosts(int $offset = 0, int $limit = Constant::FRONT_PAGE_POSTS): array
88
    {
89
        return $this->getAllPublishedPosts($offset, $limit, true);
90
    }
91
92
    /**
93
     * get the list of all the posts.
94
     * @param int $offset
95
     * @param int $limit
96
     * @return array
97
     * @throws \ErrorException
98
     */
99
    public function getPosts(int $offset = 0, int $limit = Constant::POSTS_PER_PAGE): array
100
    {
101
        return $this->getAllPublishedPosts($offset, $limit, false);
102
    }
103
104
    /**
105
     * get all the posts from a certain category
106
     * @param int $categoryId the id of the category
107
     * @param int $offset the offset for pagination
108
     * @param int $limit the limit to display
109
     * @return array list of posts in set category
110
     * @throws \Exception
111
     */
112
    public function getPostsInCategory(int $categoryId, int $offset = 0, int $limit = Constant::POSTS_PER_PAGE): array
113
    {
114
        $sql = $this->basePostSelect();
115
        $sql .= " WHERE categories_idcategories = :categoryId 
116
                ORDER BY $this->postsTbl.creation_date DESC
117
                LIMIT :limit OFFSET :offset;";
118
        $this->query($sql);
119
        $this->bind(":categoryId", $categoryId, \PDO::PARAM_INT);
120
        $this->bind(":limit", $limit);
121
        $this->bind(":offset", $offset);
122
        $this->execute();
123
124
        $results = $this->fetchAll();
125
        return $this->addExcerpt($results);
126
    }
127
128
    /**
129
     * get all the posts with a specific author
130
     * @param int $authorId
131
     * @param int $offset
132
     * @param int $limit
133
     * @return array
134
     * @throws \ErrorException
135
     */
136
    public function getPostsWithAuthor(int $authorId, int $offset = 0, int $limit = Constant::POSTS_PER_PAGE): array
137
    {
138
        $sql = $this->basePostSelect();
139
        $sql .= " WHERE author_iduser = :authorId 
140
                ORDER BY $this->postsTbl.creation_date DESC
141
                LIMIT :limit OFFSET :offset;";
142
        $this->query($sql);
143
        $this->bind(":authorId", $authorId, \PDO::PARAM_INT);
144
        $this->bind(":limit", $limit);
145
        $this->bind(":offset", $offset);
146
        $this->execute();
147
148
        $results = $this->fetchAll();
149
        return $this->addExcerpt($results);
150
    }
151
152
    /**
153
     * get a single post from it's ID
154
     * @param int $postid the post ID to get
155
     * @return array the single post details
156
     * @throws \Exception
157
     */
158
    public function getSinglePost(int $postid)
159
    {
160
        $sql = $this->basePostSelect();
161
        $sql .= " WHERE idposts = :postId;";
162
        $this->query($sql);
163
        $this->bind(":postId", $postid, \PDO::PARAM_INT);
164
        $this->execute();
165
166
        return $this->fetch();
167
    }
168
169
    /**
170
     * Create a new post
171
     * @param string $title
172
     * @param string $postImage
173
     * @param int $idCategory
174
     * @param string $article
175
     * @param int $idUser
176
     * @param int $published
177
     * @param int $onFrontPage
178
     * @param string $postSlug
179
     * @return int the id of created post
180
     * @throws \Exception
181
     */
182
    public function newPost(
183
        string $title,
184
        string $postImage,
185
        int $idCategory,
186
        string $article,
187
        int $idUser,
188
        int $published,
189
        int $onFrontPage,
190
        string $postSlug
191
    ): int {
192
        $sql = "
193
          INSERT INTO $this->postsTbl (title, post_image, categories_idcategories, article, author_iduser, creation_date, last_update, published, on_front_page, posts_slug)
194
          VALUES (:title, :post_image, :categories_idcategories, :article, :author_iduser, NOW(), NOW(), :published, :on_front_page, :posts_slug)
195
        ";
196
        $this->query($sql);
197
        $this->bind(':title', $title);
198
        $this->bind(':post_image', $postImage);
199
        $this->bind(':categories_idcategories', $idCategory);
200
        $this->bind(':article', $article);
201
        $this->bind(':author_iduser', $idUser);
202
        $this->bind(':published', $published);
203
        $this->bind(':on_front_page', $onFrontPage);
204
        $this->bind(':posts_slug', $postSlug);
205
206
        $this->execute();
207
208
        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...
209
    }
210
211
    /**
212
     * Update a post with new values
213
     * @param int $postId
214
     * @param string $title
215
     * @param string $postImage
216
     * @param int $idCategory
217
     * @param string $article
218
     * @param int $published
219
     * @param int $onFrontPage
220
     * @param string $postSlug
221
     * @return bool success
222
     * @throws \Exception
223
     */
224
    public function modifyPost(
225
        int $postId,
226
        string $title,
227
        string $postImage,
228
        int $idCategory,
229
        string $article,
230
        int $published,
231
        int $onFrontPage,
232
        string $postSlug
233
    ): bool {
234
        $sql = "
235
            UPDATE $this->postsTbl 
236
            SET 
237
                title = :title,
238
                post_image = :postImage,
239
                categories_idcategories = :idCategory,
240
                article = :article,
241
                last_update = NOW(),
242
                published = :published,
243
                on_front_page = :onFrontPage,
244
                posts_slug = :postSlug
245
            WHERE
246
              idposts = :postId
247
        ;";
248
        $this->query($sql);
249
        $this->bind(":title", $title);
250
        $this->bind(":postImage", $postImage);
251
        $this->bind(":idCategory", $idCategory);
252
        $this->bind(":article", $article);
253
        $this->bind(":published", $published);
254
        $this->bind(":onFrontPage", $onFrontPage);
255
        $this->bind(":postSlug", $postSlug);
256
        $this->bind(":postId", $postId);
257
258
        return $this->execute();
259
    }
260
261
262
}