LinkCategoryDAO::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace AL\Common\DAO;
3
4
require_once __DIR__."/../../lib/Db.php" ;
5
require_once __DIR__."/../Model/Link/LinkCategory.php" ;
6
7
/**
8
 * DAO for Link Categories
9
 */
10
class LinkCategoryDAO {
11
    private $mysqli;
12
13
    public function __construct($mysqli) {
14
        $this->mysqli = $mysqli;
15
    }
16
17
    /**
18
     * Retrieve all categories, with the count of links for each
19
     *
20
     * @return AL\Common\Model\Link\LinkCategory[] An array of all categories
21
     */
22
    public function getAllCategories() {
23
        $stmt = \AL\Db\execute_query(
24
            "LinkCategoryDAO: Get all categories",
25
            $this->mysqli,
26
            "SELECT
27
                website_category.website_category_id,
28
                website_category_name,
29
                COUNT(website_category_cross_id)
30
            FROM
31
                website_category
32
            LEFT JOIN website_category_cross
33
                ON website_category.website_category_id = website_category_cross.website_category_id
34
            GROUP BY
35
                website_category.website_category_id
36
            ORDER BY
37
                website_category_name ASC",
38
            null, null
39
        );
40
41
        \AL\Db\bind_result(
42
            "LinkCategoryDAO: Get all categories",
43
            $stmt,
44
            $id, $name, $count
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $count seems to be never defined.
Loading history...
45
        );
46
47
        $categories = [];
48
        while ($stmt->fetch()) {
49
            $categories[] = new \AL\Common\Model\Link\LinkCategory($id, $name, $count);
50
        }
51
52
        $stmt->close();
53
54
        return $categories;
55
    }
56
57
    /**
58
     * Retrieve a specific category
59
     *
60
     * @param  integer $category_id ID of the category to retrieve
61
     * @return AL\Common\Model\Link\LinkCategory Category
0 ignored issues
show
Bug introduced by
The type AL\Common\DAO\AL\Common\Model\Link\LinkCategory was not found. Did you mean AL\Common\Model\Link\LinkCategory? If so, make sure to prefix the type with \.
Loading history...
62
     */
63
    public function getCategory($category_id) {
64
        $stmt = \AL\Db\execute_query(
65
            "LinkCategoryDAO: Get single category from id: $category_id",
66
            $this->mysqli,
67
            "SELECT
68
                website_category_id,
69
                website_category_name
70
            FROM
71
                website_category
72
            WHERE
73
                website_category_id = ?",
74
            "s", $category_id
75
        );
76
77
        \AL\Db\bind_result(
78
            "LinkCategoryDAO: Get single category from id: $category_id",
79
            $stmt,
80
            $id, $name
81
        );
82
83
        $category = null;
84
        if ($stmt->fetch()) {
85
            $category = new \AL\Common\Model\Link\LinkCategory($id, $name);
86
        }
87
88
        $stmt->close();
89
90
        return $category;
91
    }
92
}
93