Passed
Push — Showing-Posts ( 80516c...edf5ba )
by Stone
01:53
created

CategoryModel::getCategories()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Core\Container;
6
use Core\Model;
7
8
class CategoryModel extends Model
9
{
10
11
    private $categoryTbl;
12
13
    public function __construct(Container $container)
14
    {
15
        parent::__construct($container);
16
17
        $this->categoryTbl = $this->getTablePrefix("categories");
18
    }
19
20
    /**
21
     * get the list of categories and return all the data
22
     * @return array the categories
23
     * @throws \ReflectionException
24
     */
25
    public function getCategories()
26
    {
27
        return $this->getResultSet('categories');
28
    }
29
30
    /**
31
     * get all the menu elements from the database
32
     * @return array the categories and access URL
33
     * @throws \ReflectionException
34
     */
35
    public function getMenu(): array
36
    {
37
        $data = [];
38
        //get the categories from database
39
        $categories = $this->getCategories();
40
        foreach ($categories as $category) {
41
            $data += [
42
                $category->category_name => '/category/posts/' . $category->categories_slug
43
            ];
44
        }
45
        return $data;
46
    }
47
48
    /**
49
     * get all the details from the category table
50
     * @param int $categoryId
51
     * @return array
52
     * @throws \ReflectionException
53
     */
54
    public function getCategoryDetails(int $categoryId)
55
    {
56
        return $this->getRowById($categoryId, "categories");
57
    }
58
59
    /**
60
     * count the total number of categories
61
     * @return int
62
     * @throws \Exception
63
     */
64
    public function countCategories(): int
65
    {
66
        $sql = "SELECT COUNT(*) FROM $this->categoryTbl";
67
        $this->query($sql);
68
        $this->execute();
69
        return $this->stmt->fetchColumn();
70
    }
71
72
    /**
73
     * get the list of categories with pagination
74
     * @param int $offset
75
     * @param int $limit
76
     * @return array
77
     * @throws \Exception
78
     */
79
    public function getCategoryList(int $offset = 0, int $limit = Constant::POSTS_PER_PAGE)
0 ignored issues
show
Bug introduced by
The type App\Models\Constant was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
80
    {
81
        $sql = "
82
            SELECT * FROM $this->categoryTbl 
83
            LIMIT :limit OFFSET :offset
84
        ";
85
        $this->query($sql);
86
        $this->bind(":limit", $limit);
87
        $this->bind(":offset", $offset);
88
        $this->execute();
89
        return $this->fetchAll();
90
    }
91
92
    /**
93
     * Update a specific category
94
     * @param int $categoryId
95
     * @param string $categoryName
96
     * @param string $categorySlug
97
     * @return bool
98
     * @throws \Exception
99
     */
100
    public function update(int $categoryId, string $categoryName, string $categorySlug)
101
    {
102
        $sql = "
103
            UPDATE $this->categoryTbl
104
            SET
105
              category_name = :categoryName,
106
              categories_slug = :categorySlug
107
            WHERE
108
              idcategories = :categoryId
109
        ";
110
        $this->query($sql);
111
        $this->bind(":categoryName", $categoryName);
112
        $this->bind(":categorySlug", $categorySlug);
113
        $this->bind(":categoryId", $categoryId);
114
        return $this->execute();
115
    }
116
117
    /**
118
     * Create a new category
119
     * @param string $categoryName
120
     * @param string $categorySlug
121
     * @return bool
122
     * @throws \Exception
123
     */
124
    public function new(string $categoryName, string $categorySlug)
125
    {
126
        $sql = "
127
            INSERT INTO $this->categoryTbl (category_name, categories_slug)
128
            VALUES (:categoryName, :categorySlug)
129
        ";
130
131
        $this->query($sql);
132
        $this->bind(":categoryName", $categoryName);
133
        $this->bind(":categorySlug", $categorySlug);
134
        return $this->execute();
135
    }
136
137
    /**
138
     * delete a category by Id
139
     * @param int $categoryId
140
     * @return bool
141
     * @throws \Exception
142
     */
143
    public function delete(int $categoryId)
144
    {
145
        $sql = "
146
        DELETE FROM $this->categoryTbl 
147
        WHERE idcategories = :categoryId
148
        ";
149
        $this->query($sql);
150
        $this->bind(":categoryId", $categoryId);
151
        return $this->execute();
152
    }
153
154
155
    /**
156
     * return the name of the category from the ID
157
     * @param int $categoryId
158
     * @return mixed
159
     * @throws \Exception
160
     */
161
    public function getNameFromId(int $categoryId)
162
    {
163
        $sql = "SELECT category_name from $this->categoryTbl WHERE idcategories = :categoryId";
164
        $this->query($sql);
165
        $this->bind(":categoryId", $categoryId);
166
        $this->execute();
167
        return $this->stmt->fetchColumn();
168
    }
169
170
171
172
}