Passed
Push — Showing-Posts ( 4125ee...65a5c0 )
by Stone
02:18
created

PostModel::newPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 8
dl 0
loc 27
rs 9.8333
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
     * get all the posts with details
28
     * @param int $offset where to start (for pagination)
29
     * @param int $limit the number of posts
30
     * @param bool $isFrontPage extract only front page posts
31
     * @return array list of posts
32
     * @throws \ErrorException
33
     */
34
    private function getAllPosts(int $offset, int $limit, bool $isFrontPage = false)
35
    {
36
        $sql = "SELECT title, post_image,article,$this->postsTbl.last_update, posts_slug, category_name, categories_slug, pseudo as author, idusers
37
                FROM $this->postsTbl INNER JOIN $this->categoriesTbl ON $this->postsTbl.categories_idcategories = $this->categoriesTbl.idcategories
38
                INNER JOIN $this->usersTbl ON $this->postsTbl.author_iduser = $this->usersTbl.idusers";
39
        if ($isFrontPage) {
40
            $sql .= " WHERE on_front_page = 1";
41
        }
42
        $sql .= " ORDER BY $this->postsTbl.creation_date DESC";
43
        $sql .= " LIMIT $limit OFFSET $offset";
44
        $this->query($sql);
45
        $this->execute();
46
        $results = $this->fetchAll();
47
        $sendResults = [];
48
        //we create the excerpt for the text and add it to the object
49
        foreach ($results as $result) {
50
            $result->{'excerpt'} = $this->getExcerpt($result->article);
51
            $sendResults[] = $result;
52
        }
53
        return $sendResults;
54
    }
55
56
    /**
57
     * get the list of front posts
58
     * @param int $offset
59
     * @param int $limit
60
     * @return array
61
     * @throws \ErrorException
62
     */
63
    public function getFrontPosts(int $offset = 0, int $limit = Constant::FRONT_PAGE_POSTS)
64
    {
65
        return $this->getAllPosts($offset, $limit, true);
66
    }
67
68
    /**
69
     * get the list of all the posts.
70
     * @param int $offset
71
     * @param int $limit
72
     * @return array
73
     * @throws \ErrorException
74
     */
75
    public function getPosts(int $offset = 0, int $limit = Constant::POSTS_PER_PAGE)
76
    {
77
        return $this->getAllPosts($offset, $limit, false);
78
    }
79
80
81
    /**
82
     * Create a new post
83
     * @param string $title
84
     * @param string $postImage
85
     * @param int $idCategory
86
     * @param string $article
87
     * @param int $idUser
88
     * @param int $published
89
     * @param int $onFrontPage
90
     * @param string $postSlug
91
     * @return string
92
     */
93
    public function newPost(
94
        string $title,
95
        string $postImage,
96
        int $idCategory,
97
        string $article,
98
        int $idUser,
99
        int $published,
100
        int $onFrontPage,
101
        string $postSlug
102
    ) {
103
        $sql = "
104
          INSERT INTO $this->postsTbl (title, post_image, categories_idcategories, article, author_iduser, creation_date, published, on_front_page, posts_slug)
105
          VALUES (:title, :post_image, :categories_idcategories, :article, :author_iduser, NOW(), :published, :on_front_page, :posts_slug)
106
        ";
107
        $this->query($sql);
108
        $this->bind(':title', $title);
109
        $this->bind(':post_image', $postImage);
110
        $this->bind(':categories_idcategories', $idCategory);
111
        $this->bind(':article', $article);
112
        $this->bind(':author_iduser', $idUser);
113
        $this->bind(':published', $published);
114
        $this->bind(':on_front_page', $onFrontPage);
115
        $this->bind(':posts_slug', $postSlug);
116
117
        $this->execute();
118
119
        return $this->dbh->lastInsertId();
120
121
    }
122
123
    public function getPostsInCategory(int $categoryId):array
124
    {
125
        $sql = "SELECT * FROM $this->postsTbl WHERE categories_idcategories = :categoryId;";
126
        $this->query($sql);
127
        $this->bind(":categoryId", $categoryId, \PDO::PARAM_INT);
128
        $this->execute();
129
130
        return $this->fetchAll();
131
    }
132
133
}