LinkDAO   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 156
dl 0
loc 262
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllLinks() 0 33 2
A getLinkQuery() 0 29 2
A getAllLinksForCategory() 0 43 2
A __construct() 0 2 1
A getLinkCount() 0 27 2
B getRandomLink() 0 82 3
1
<?php
2
namespace AL\Common\DAO;
3
4
require_once __DIR__."/../../lib/Db.php" ;
5
require_once __DIR__."/../Model/Link/Link.php" ;
6
7
/**
8
 * DAO for Links
9
 */
10
class LinkDAO {
11
    private $mysqli;
12
13
    public function __construct($mysqli) {
14
        $this->mysqli = $mysqli;
15
    }
16
17
    /**
18
     * Get the SQL query to retrieve links, either all links or links for a
19
     * specific category
20
     *
21
     * @param  integer $category_id Optional ID of the category to retrieve links for
22
     * @return string SQL query
23
     */
24
    private function getLinkQuery($category_id = null) {
25
        $query = "SELECT
26
                website.website_id,
27
                website_name,
28
                website_url,
29
                description,
30
                website_imgext,
31
                website.inactive,
32
                users.userid,
33
                users.user_id,
34
                website_date,
35
                website_category.website_category_id,
36
                website_category.website_category_name
37
            FROM
38
                website
39
            LEFT JOIN website_category_cross
40
                ON website_category_cross.website_id = website.website_id
41
            LEFT JOIN website_category
42
                ON website_category.website_category_id = website_category_cross.website_category_id
43
            LEFT JOIN users
44
                ON users.user_id = website.user_id";
45
46
        if (isset($category_id)) {
47
            $query .= " WHERE website_category.website_category_id = ?";
48
        }
49
50
        $query .= " ORDER BY website_date DESC LIMIT ?, ?";
51
52
        return $query;
53
    }
54
55
    /**
56
     * Retrieve all links
57
     *
58
     * @param  integer $offset Link offset to start with, for paging
59
     * @param  integer $limit  How many links to return, for paging
60
     * @return \AL\Common\Model\Link\Link[] An array of links
61
     */
62
    public function getAllLinks($offset = 0, $limit = 5) {
63
        $stmt = \AL\Db\execute_query(
64
            "LinkDAO: Get all links",
65
            $this->mysqli,
66
            $this->getLinkQuery(),
67
            "ii", $offset, $limit
68
        );
69
70
        \AL\Db\bind_result(
71
            "LinkDAO: Get all links",
72
            $stmt,
73
            $id, $name, $url, $description, $imgext, $inactive, $user, $userid, $date, $category_id, $category_name
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $inactive seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $url seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $imgext seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $userid seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $category_id seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $user seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $date seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $category_name seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $description seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
74
        );
75
76
        $links = [];
77
        while ($stmt->fetch()) {
78
            $links[] = new \AL\Common\Model\Link\Link(
79
                $id,
80
                $name,
81
                $url,
82
                $description,
83
                $imgext,
84
                $inactive,
85
                $user,
86
                $date,
87
                $userid,
88
                $category_name
89
            );
90
        }
91
92
        $stmt->close();
93
94
        return $links;
95
    }
96
97
    /**
98
     * Retrieve all links for a category
99
     *
100
     * @param  integer $category_id ID of the category to retrieve links for
101
     * @param  integer $offset      Link offset to start with, for paging
102
     * @param  ingeter $limit       How many links to return, for paging
0 ignored issues
show
Bug introduced by
The type AL\Common\DAO\ingeter 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...
103
     * @return \AL\Common\Model\Link\Link[] An array of links
104
     */
105
    public function getAllLinksForCategory($category_id, $offset = 0, $limit = 5) {
106
        $stmt = \AL\Db\execute_query(
107
            "LinkDAO: Get all links",
108
            $this->mysqli,
109
            $this->getLinkQuery($category_id),
110
            "iii", $category_id, $offset, $limit
111
        );
112
113
        \AL\Db\bind_result(
114
            "LinkDAO: Get all links",
115
            $stmt,
116
            $id,
117
            $name,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
118
            $url,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $url seems to be never defined.
Loading history...
119
            $description,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $description seems to be never defined.
Loading history...
120
            $imgext,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $imgext seems to be never defined.
Loading history...
121
            $inactive,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $inactive seems to be never defined.
Loading history...
122
            $user,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user seems to be never defined.
Loading history...
123
            $userid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userid seems to be never defined.
Loading history...
124
            $date,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $date seems to be never defined.
Loading history...
125
            $category_id,
126
            $category_name
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $category_name does not exist. Did you maybe mean $category_id?
Loading history...
127
        );
128
129
        $links = [];
130
        while ($stmt->fetch()) {
131
            $links[] = new \AL\Common\Model\Link\Link(
132
                $id,
133
                $name,
134
                $url,
135
                $description,
136
                $imgext,
137
                $inactive,
138
                $user,
139
                $date,
140
                $userid,
141
                $category_name
142
            );
143
        }
144
145
        $stmt->close();
146
147
        return $links;
148
    }
149
150
    /**
151
     * Get the total count of links, for all links or for a given category
152
     *
153
     * @param  integer $category_id Optional ID of a category to count links for
154
     * @return integer Number of links
155
     */
156
    public function getLinkCount($category_id = null) {
157
        if (isset($category_id)) {
158
            $stmt = \AL\Db\execute_query(
159
                "LinkDAO: Get link count for category $category_id",
160
                $this->mysqli,
161
                "SELECT COUNT(*) FROM website_category_cross WHERE website_category_id = ?",
162
                "i", $category_id
163
            );
164
        } else {
165
            $stmt = \AL\Db\execute_query(
166
                "LinkDAO: Get link count",
167
                $this->mysqli,
168
                "SELECT COUNT(*) FROM website".
169
                null, null
170
            );
171
        }
172
173
        \AL\Db\bind_result(
174
            "LinkDAO: Get link count",
175
            $stmt,
176
            $count
177
        );
178
179
        $stmt->fetch();
180
        $stmt->close();
181
182
        return $count;
183
    }
184
185
    /**
186
     * Get a random link
187
     * I have excluded youtube links in here as the youtube logo's do not fit the look
188
     * and it bothered me.
189
     */
190
    public function getRandomLink($ignore_category = null) {
191
        if (isset($ignore_category)) {
192
            $stmt = \AL\Db\execute_query(
193
                "LinkDAO: Get random link",
194
                $this->mysqli,
195
                "SELECT website.website_id,
196
                        website.website_name,
197
                        website.website_url,
198
                        website.website_imgext,
199
                        website.website_date,
200
                        website.description,
201
                        website.inactive,
202
                        users.userid,
203
                        users.user_id
204
                        FROM website
205
                        LEFT JOIN website_category_cross ON 
206
                        (website.website_id = website_category_cross.website_id)
207
                        LEFT JOIN website_category ON 
208
                        (website_category.website_category_id = website_category_cross.website_category_id)
209
                        LEFT JOIN users ON ( website.user_id = users.user_id )
210
                        WHERE website.website_imgext <> ' ' and website.inactive = 0 
211
                        and website_category.website_category_name <> ?
212
                        ORDER BY RAND() LIMIT 1", "s", $ignore_category
213
            );
214
        } else {
215
            $stmt = \AL\Db\execute_query(
216
                "LinkDAO: Get random link",
217
                $this->mysqli,
218
                "SELECT website.website_id,
219
                        website.website_name,
220
                        website.website_url,
221
                        website.website_imgext,
222
                        website.website_date,
223
                        website.description,
224
                        website.inactive,
225
                        users.userid,
226
                        users.user_id
227
                        FROM website
228
                        LEFT JOIN website_category_cross ON 
229
                        (website.website_id = website_category_cross.website_id)
230
                        LEFT JOIN website_category ON 
231
                        (website_category.website_category_id = website_category_cross.website_category_id)
232
                        LEFT JOIN users ON ( website.user_id = users.user_id )
233
                        WHERE website.website_imgext <> ' ' and website.inactive = 0
234
                        ORDER BY RAND() LIMIT 1".
235
                         null, null
236
            );
237
        }
238
        
239
        \AL\Db\bind_result(
240
            "LinkDAO: Get random link",
241
            $stmt,
242
            $id,
243
            $name,
244
            $url,
245
            $imgext,
246
            $date,
247
            $description,
248
            $inactive,
249
            $user,
250
            $userid
251
        );
252
253
        $link = null;
254
        if ($stmt->fetch()) {
255
            $link = new \AL\Common\Model\Link\Link(
256
                $id,
257
                $name,
258
                $url,
259
                $description,
260
                $imgext,
261
                $inactive,
262
                $user,
263
                $date,
264
                $userid,
265
                null
266
            );
267
        }
268
269
        $stmt->close();
270
271
        return $link;
272
    }
273
}
274