Passed
Push — Showing-Posts ( 1ec90b )
by Stone
02:05
created

PostModel::getAllPosts()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 4
nop 3
dl 0
loc 22
rs 9.7
c 0
b 0
f 0
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
    private function getAllPosts(int $offset, int $limit, bool $isFrontPage = false){
14
        $postsTbl = $this->getTablePrefix('posts');
15
        $categoriesTbl = $this->getTablePrefix('categories');
16
        $usersTbl = $this->getTablePrefix('users');
17
18
        $sql = "SELECT title, post_image,article,$postsTbl.last_update, posts_slug, category_name, categories_slug, pseudo as author, idusers
19
                FROM $postsTbl INNER JOIN $categoriesTbl ON $postsTbl.categories_idcategories = $categoriesTbl.idcategories
20
                INNER JOIN $usersTbl ON $postsTbl.author_iduser = $usersTbl.idusers";
21
        if($isFrontPage){
22
            $sql .= " WHERE on_front_page = 1";
23
        }
24
        $sql .=" LIMIT $limit OFFSET $offset";
25
        $this->query($sql);
26
        $this->execute();
27
        $results = $this->fetchAll();
28
        $sendResults= [];
29
        //we create the excerpt for the text and add it to the object
30
        foreach ($results as $result){
31
            $result->{'excerpt'} = $this->getExcerpt($result->article);
32
            $sendResults[] = $result;
33
        }
34
        return $sendResults;
35
    }
36
37
    public function getFrontPosts(int $offset = 0, int $limit = Constant::FRONT_PAGE_POSTS)
38
    {
39
        return $this->getAllPosts($offset, $limit, true);
40
    }
41
42
    public function getPosts(int $offset = 0, int $limit = Constant::POSTS_PER_PAGE){
43
        return $this->getAllPosts($offset, $limit, false);
44
    }
45
}