Weight   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 40
c 1
b 0
f 0
dl 0
loc 84
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A read() 0 17 2
A create() 0 25 2
A delete() 0 22 2
1
<?php
2
//error_reporting(0);
3
class Weight {
4
5
    private $conn;
6
    private $db_table = "weights";
7
8
    public $id;
9
    public $userid;
10
    public $weight;
11
    public $measuredate;
12
13
    public function __construct($db) {
14
        $this->conn = $db;
15
    }
16
17
    public function read($amount = false, $order = 'DESC') {
18
19
        $query = "
20
        SELECT ID as id, Weight as weight, MeasureDate as measuredate, CreationDate as creationdate
21
        FROM ". $this->db_table . "
22
        WHERE UserID = :userid
23
        ORDER BY CreationDate ". $order;
24
25
        if ($amount) {
26
            $query .= " LIMIT " . $amount;
27
        }
28
29
        $stmt = $this->conn->prepare($query);
30
        $stmt->bindParam(':userid', $this->userid);
31
        $stmt->execute();
32
33
        return $stmt;
34
35
    }
36
37
    public function create() {
38
39
        $query = "
40
            INSERT INTO " . $this->db_table . " SET
41
            UserID = :userid,
42
            Weight = :weight,
43
            MeasureDate = :measuredate
44
        ";
45
46
        $this->userid = htmlspecialchars(strip_tags($this->userid));
47
        $this->weight = htmlspecialchars(strip_tags($this->weight));
48
        $this->measuredate = htmlspecialchars(strip_tags($this->measuredate));
49
50
        $stmt = $this->conn->prepare($query);
51
        $stmt->bindParam(":userid", $this->userid);
52
        $stmt->bindParam(":weight", $this->weight);
53
        $stmt->bindParam(":measuredate", $this->measuredate);
54
55
56
        if ($stmt->execute()) {
57
            $this->id = $this->conn->lastInsertId();
58
            return true;
59
        }
60
61
        return false;
62
63
    }
64
65
    public function delete() {
66
67
        $query = "
68
        DELETE FROM " . $this->db_table . "
69
        WHERE ID = :id AND UserID = :userid
70
        ";
71
72
        $this->id = htmlspecialchars(strip_tags($this->id));
73
        $this->userid = htmlspecialchars(strip_tags($this->userid));
74
75
        $stmt = $this->conn->prepare($query);
76
        $stmt->bindParam(":id", $this->id);
77
        $stmt->bindParam(":userid", $this->userid);
78
79
80
        if ($stmt->execute()) {
81
82
            return true;
83
84
        }
85
86
        return false;
87
88
    }
89
90
}
91