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

PaginationModel::totalNumberPostsInCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Core\Model;
6
use Core\Container;
7
8
class PaginationModel extends Model{
9
10
    private $postsTbl;
11
12
    public function __construct(Container $container)
13
    {
14
        parent::__construct($container);
15
        $this->postsTbl = $this->getTablePrefix('posts');
16
    }
17
18
19
    /**
20
     * get the total number of posts
21
     * @return int
22
     * @throws \Exception
23
     */
24
    public function totalNumberPosts(): int
25
    {
26
        $sql = "SELECT COUNT(*) FROM $this->postsTbl WHERE published = 1";
27
        $this->query($sql);
28
        $this->execute();
29
        return $this->stmt->fetchColumn();
30
    }
31
32
    /**
33
     * get the total number of posts in a category
34
     * @param int $categoryId
35
     * @return int
36
     * @throws \Exception
37
     */
38
    public function totalNumberPostsInCategory(int $categoryId): int
39
    {
40
        $sql = "SELECT COUNT(*) FROM $this->postsTbl WHERE published = 1 AND categories_idcategories = :categoryId ";
41
        $this->query($sql);
42
        $this->bind(":categoryId", $categoryId, \PDO::PARAM_INT);
43
        $this->execute();
44
        return $this->stmt->fetchColumn();
45
    }
46
47
    /**
48
     * get the total number of posts in a category
49
     * @param int $authorid
50
     * @return int
51
     * @throws \Exception
52
     */
53
    public function totalNumberPostsByAuthor(int $authorid):int
54
    {
55
        $sql = "SELECT COUNT(*) FROM $this->postsTbl WHERE published = 1 AND author_iduser = :authorId ";
56
        $this->query($sql);
57
        $this->bind(":authorId", $authorid, \PDO::PARAM_INT);
58
        $this->execute();
59
        return $this->stmt->fetchColumn();
60
    }
61
}