Passed
Push — Showing-Posts ( d80634...b1a8d3 )
by Stone
03:49 queued 01:15
created

PostModel::modifyPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 8
dl 0
loc 36
rs 9.8666
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 getAllPosts(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->getAllPosts($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->getAllPosts($offset, $limit, false);
102
    }
103
104
105
    /**
106
     * Create a new post
107
     * @param string $title
108
     * @param string $postImage
109
     * @param int $idCategory
110
     * @param string $article
111
     * @param int $idUser
112
     * @param int $published
113
     * @param int $onFrontPage
114
     * @param string $postSlug
115
     * @return int the id of created post
116
     * @throws \Exception
117
     */
118
    public function newPost(
119
        string $title,
120
        string $postImage,
121
        int $idCategory,
122
        string $article,
123
        int $idUser,
124
        int $published,
125
        int $onFrontPage,
126
        string $postSlug
127
    ):int
128
    {
129
        $sql = "
130
          INSERT INTO $this->postsTbl (title, post_image, categories_idcategories, article, author_iduser, creation_date, last_update, published, on_front_page, posts_slug)
131
          VALUES (:title, :post_image, :categories_idcategories, :article, :author_iduser, NOW(), NOW(), :published, :on_front_page, :posts_slug)
132
        ";
133
        $this->query($sql);
134
        $this->bind(':title', $title);
135
        $this->bind(':post_image', $postImage);
136
        $this->bind(':categories_idcategories', $idCategory);
137
        $this->bind(':article', $article);
138
        $this->bind(':author_iduser', $idUser);
139
        $this->bind(':published', $published);
140
        $this->bind(':on_front_page', $onFrontPage);
141
        $this->bind(':posts_slug', $postSlug);
142
143
        $this->execute();
144
145
        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...
146
    }
147
148
    /**
149
     * Update a post with new values
150
     * @param int $postId
151
     * @param string $title
152
     * @param string $postImage
153
     * @param int $idCategory
154
     * @param string $article
155
     * @param int $published
156
     * @param int $onFrontPage
157
     * @param string $postSlug
158
     * @return bool success
159
     * @throws \Exception
160
     */
161
    public function modifyPost(
162
        int $postId,
163
        string $title,
164
        string $postImage,
165
        int $idCategory,
166
        string $article,
167
        int $published,
168
        int $onFrontPage,
169
        string $postSlug
170
    ):bool
171
    {
172
        $sql = "
173
            UPDATE $this->postsTbl 
174
            SET 
175
                title = :title,
176
                post_image = :postImage,
177
                categories_idcategories = :idCategory,
178
                article = :article,
179
                last_update = NOW(),
180
                published = :published,
181
                on_front_page = :onFrontPage,
182
                posts_slug = :postSlug
183
            WHERE
184
              idposts = :postId
185
        ;";
186
        $this->query($sql);
187
        $this->bind(":title", $title);
188
        $this->bind(":postImage", $postImage);
189
        $this->bind(":idCategory", $idCategory);
190
        $this->bind(":article", $article);
191
        $this->bind(":published", $published);
192
        $this->bind(":onFrontPage", $onFrontPage);
193
        $this->bind(":postSlug", $postSlug);
194
        $this->bind(":postId", $postId);
195
196
        return $this->execute();
197
    }
198
199
    /**
200
     * get all the posts from a certain category
201
     * @param int $categoryId the id of the category
202
     * @param int $offset the offset for pagination
203
     * @param int $limit the limit to display
204
     * @return array list of posts in set category
205
     * @throws \Exception
206
     */
207
    public function getPostsInCategory(int $categoryId, int $offset = 0, int $limit = Constant::POSTS_PER_PAGE): array
208
    {
209
        $sql = $this->basePostSelect();
210
        $sql .= " WHERE categories_idcategories = :categoryId 
211
                ORDER BY $this->postsTbl.creation_date DESC
212
                LIMIT :limit OFFSET :offset;";
213
        $this->query($sql);
214
        $this->bind(":categoryId", $categoryId, \PDO::PARAM_INT);
215
        $this->bind(":limit", $limit);
216
        $this->bind(":offset", $offset);
217
        $this->execute();
218
219
        $results = $this->fetchAll();
220
        $sendResults = [];
221
        //we create the excerpt for the text and add it to the object
222
        foreach ($results as $result) {
223
            $result->{'excerpt'} = $this->getExcerpt($result->article);
224
            $sendResults[] = $result;
225
        }
226
        return $sendResults;
227
    }
228
229
230
    /**
231
     * get a single post from it's ID
232
     * @param int $postid the post ID to get
233
     * @return array the single post details
234
     * @throws \Exception
235
     */
236
    public function getSinglePost(int $postid):array
237
    {
238
        $sql = $this->basePostSelect();
239
        $sql .= " WHERE idposts = :postId;";
240
        $this->query($sql);
241
        $this->bind(":postId", $postid, \PDO::PARAM_INT);
242
        $this->execute();
243
244
        return $this->fetch();
245
    }
246
}