Passed
Push — Showing-Posts ( 7278ae...35fb9c )
by Stone
01:54
created

PostModel::newPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 8
dl 0
loc 29
rs 9.7998
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\Model;
7
use Core\Traits\StringFunctions;
8
9
class PostModel extends Model
10
{
11
    use StringFunctions;
12
13
    /**
14
     * get all the posts with details
15
     * @param int $offset where to start (for pagination)
16
     * @param int $limit the number of posts
17
     * @param bool $isFrontPage extract only front page posts
18
     * @return array list of posts
19
     * @throws \ErrorException
20
     */
21
    private function getAllPosts(int $offset, int $limit, bool $isFrontPage = false)
22
    {
23
        $postsTbl = $this->getTablePrefix('posts');
24
        $categoriesTbl = $this->getTablePrefix('categories');
25
        $usersTbl = $this->getTablePrefix('users');
26
27
        $sql = "SELECT title, post_image,article,$postsTbl.last_update, posts_slug, category_name, categories_slug, pseudo as author, idusers
28
                FROM $postsTbl INNER JOIN $categoriesTbl ON $postsTbl.categories_idcategories = $categoriesTbl.idcategories
29
                INNER JOIN $usersTbl ON $postsTbl.author_iduser = $usersTbl.idusers";
30
        if ($isFrontPage) {
31
            $sql .= " WHERE on_front_page = 1";
32
        }
33
        $sql .= " LIMIT $limit OFFSET $offset";
34
        $this->query($sql);
35
        $this->execute();
36
        $results = $this->fetchAll();
37
        $sendResults = [];
38
        //we create the excerpt for the text and add it to the object
39
        foreach ($results as $result) {
40
            $result->{'excerpt'} = $this->getExcerpt($result->article);
41
            $sendResults[] = $result;
42
        }
43
        return $sendResults;
44
    }
45
46
    /**
47
     * get the list of front posts
48
     * @param int $offset
49
     * @param int $limit
50
     * @return array
51
     * @throws \ErrorException
52
     */
53
    public function getFrontPosts(int $offset = 0, int $limit = Constant::FRONT_PAGE_POSTS)
54
    {
55
        return $this->getAllPosts($offset, $limit, true);
56
    }
57
58
    /**
59
     * get the list of all the posts.
60
     * @param int $offset
61
     * @param int $limit
62
     * @return array
63
     * @throws \ErrorException
64
     */
65
    public function getPosts(int $offset = 0, int $limit = Constant::POSTS_PER_PAGE)
66
    {
67
        return $this->getAllPosts($offset, $limit, false);
68
    }
69
70
71
    public function newPost(
72
        string $title,
73
        string $postImage,
74
        int $idCategory,
75
        string $article,
76
        int $idUser,
77
        int $published,
78
        int $onFrontPage,
79
        string $postSlug
80
    ) {
81
82
        $postsTbl = $this->getTablePrefix('posts');
83
        $sql = "
84
          INSERT INTO $postsTbl (title, post_image, categories_idcategories, article, author_iduser, creation_date, published, on_front_page, posts_slug)
85
          VALUES (:title, :post_image, :categories_idcategories, :article, :author_iduser, NOW(), :published, :on_front_page, :posts_slug)
86
        ";
87
        $stmt = $this->dbh->prepare($sql);
88
        $stmt->bindValue(':title', $title);
89
        $stmt->bindValue(':post_image', $postImage);
90
        $stmt->bindValue(':categories_idcategories', $idCategory);
91
        $stmt->bindValue(':article', $article);
92
        $stmt->bindValue(':author_iduser', $idUser);
93
        $stmt->bindValue(':published', $published);
94
        $stmt->bindValue(':on_front_page', $onFrontPage);
95
        $stmt->bindValue(':posts_slug', $postSlug);
96
97
        $stmt->execute();
98
99
        return $this->dbh->lastInsertId();
100
101
    }
102
103
}