ArticleDAO   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A getLatestArticles() 0 41 2
1
<?php
2
namespace AL\Common\DAO;
3
4
require_once __DIR__."/../../lib/Db.php" ;
5
require_once __DIR__."/../Model/Article/Article.php" ;
6
7
/**
8
 * DAO for Article
9
 */
10
class ArticleDAO {
11
    private $mysqli;
12
13
    public function __construct($mysqli) {
14
        $this->mysqli = $mysqli;
15
    }
16
17
    /**
18
     * Return the latest Articles, sorted by descending date
19
     *
20
     * @param integer $limit How many articles to return
21
     * @return \AL\Common\Model\Article\Article[] An array of Articles
22
     */
23
    public function getLatestArticles($limit = 20) {
24
        $stmt = \AL\Db\execute_query(
25
            "ArticleDAO: getLatestArticles",
26
            $this->mysqli,
27
            "SELECT
28
                article_main.article_id,
29
                article_text.article_title,
30
                article_text.article_intro,
31
                article_text.article_date,
32
                users.userid
33
            FROM
34
                article_main
35
            LEFT JOIN article_text ON article_text.article_id = article_main.article_id
36
            LEFT JOIN users ON article_main.user_id = users.user_id
37
            ORDER BY
38
                article_date
39
            DESC
40
            LIMIT ?",
41
            "i", $limit
42
        );
43
44
        \AL\Db\bind_result(
45
            "ArticleDAO: getLatestArticles",
46
            $stmt,
47
            $id, $title, $intro, $date, $user
48
        );
49
50
        $articles = [];
51
        while ($stmt->fetch()) {
52
            $articles[] = new \AL\Common\Model\Article\Article(
53
                $id,
54
                $title,
55
                $intro,
56
                $date,
57
                $user
58
            );
59
        }
60
61
        $stmt->close();
62
63
        return $articles;
64
    }
65
}
66