Passed
Pull Request — Showing-Posts (#54)
by Stone
01:57
created

CategoryModel::getMenu()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A CategoryModel::getCategoryDetails() 0 3 1
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 details from the category table
32
     * @param int $categoryId
33
     * @return array
34
     * @throws \ReflectionException
35
     */
36
    public function getCategoryDetails(int $categoryId)
37
    {
38
        return $this->getRowById($categoryId, "categories");
39
    }
40
41
    /**
42
     * count the total number of categories
43
     * @return int
44
     * @throws \Exception
45
     */
46
    public function countCategories(): int
47
    {
48
        return $this->count('categories');
49
    }
50
51
    /**
52
     * get the list of categories with pagination
53
     * @param int $offset
54
     * @param int $limit
55
     * @return array
56
     * @throws \Exception
57
     */
58
    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...
59
    {
60
        return $this->list($offset, $limit, 'categories');
61
    }
62
63
    /**
64
     * Update a specific category
65
     * @param int $categoryId
66
     * @param string $categoryName
67
     * @param string $categorySlug
68
     * @return bool
69
     * @throws \Exception
70
     */
71
    public function update(int $categoryId, string $categoryName, string $categorySlug)
72
    {
73
        $sql = "
74
            UPDATE $this->categoryTbl
75
            SET
76
              category_name = :categoryName,
77
              categories_slug = :categorySlug
78
            WHERE
79
              idcategories = :categoryId
80
        ";
81
        $this->query($sql);
82
        $this->bind(":categoryName", $categoryName);
83
        $this->bind(":categorySlug", $categorySlug);
84
        $this->bind(":categoryId", $categoryId);
85
        return $this->execute();
86
    }
87
88
    /**
89
     * Create a new category
90
     * @param string $categoryName
91
     * @param string $categorySlug
92
     * @return bool
93
     * @throws \Exception
94
     */
95
    public function new(string $categoryName, string $categorySlug)
96
    {
97
        $sql = "
98
            INSERT INTO $this->categoryTbl (category_name, categories_slug)
99
            VALUES (:categoryName, :categorySlug)
100
        ";
101
102
        $this->query($sql);
103
        $this->bind(":categoryName", $categoryName);
104
        $this->bind(":categorySlug", $categorySlug);
105
        return $this->execute();
106
    }
107
108
    /**
109
     * delete a category by Id
110
     * @param int $categoryId
111
     * @return bool
112
     * @throws \Exception
113
     */
114
    public function delete(int $categoryId)
115
    {
116
        $sql = "
117
        DELETE FROM $this->categoryTbl 
118
        WHERE idcategories = :categoryId
119
        ";
120
        $this->query($sql);
121
        $this->bind(":categoryId", $categoryId);
122
        return $this->execute();
123
    }
124
125
126
    /**
127
     * return the name of the category from the ID
128
     * @param int $categoryId
129
     * @return mixed
130
     * @throws \Exception
131
     */
132
    public function getNameFromId(int $categoryId)
133
    {
134
        $sql = "SELECT category_name from $this->categoryTbl WHERE idcategories = :categoryId";
135
        $this->query($sql);
136
        $this->bind(":categoryId", $categoryId);
137
        $this->execute();
138
        return $this->stmt->fetchColumn();
139
    }
140
141
    /**
142
     * get the category slug from the ID
143
     * @param int $categoryId
144
     * @return string
145
     * @throws \ReflectionException
146
     */
147
    public function getCategorySlugFromId(int $categoryId): string
148
    {
149
        return $this->getSlugFromId($categoryId, "idcategories", "categories_slug", "categories");
150
    }
151
152
    /**
153
     * check if category slug is unique
154
     * @param string $categorySlug
155
     * @return bool
156
     * @throws \Exception
157
     */
158
    public function isCategorySlugUnique(string $categorySlug)
159
    {
160
        return $this->isSlugUnique($categorySlug, "categories_slug", "categories");
161
    }
162
163
    /**
164
     * Get the category ID from a slug
165
     * @param string $categorySlug
166
     * @return int
167
     * @throws \Exception
168
     */
169
    public function getCategoryIdFromSlug(string $categorySlug): int
170
    {
171
        return $this->getIdFromSlug($categorySlug, "idcategories", "categories_slug", "categories");
172
    }
173
174
175
}