User   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 82
c 3
b 0
f 0
dl 0
loc 142
rs 10
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
B create() 0 42 9
A emailExists() 0 32 2
1
<?php
2
3
//error_reporting(0);
4
5
class User {
6
7
    private $conn;
8
    private $db_table = "users";
9
10
    public $id;
11
    public $language;
12
    public $firstname;
13
    public $lastname;
14
    public $email;
15
    public $password;
16
    public $isFemale;
17
    public $height;
18
    public $birthdate;
19
    public $aims;
20
21
    public function __construct($db) {
22
        $this->conn = $db;
23
    }
24
25
    public function create() {
26
27
        $query = "
28
        INSERT INTO " . $this->db_table . " SET
29
        Firstname = :firstname,
30
        Lastname = :lastname,
31
        Email = :email,
32
        Password = :password";
33
34
        $stmt = $this->conn->prepare($query);
35
36
        if (strlen($this->firstname)>0 && strlen($this->lastname)>0) {
37
            $this->firstname = htmlspecialchars(strip_tags($this->firstname));
38
            $this->lastname = htmlspecialchars(strip_tags($this->lastname));
39
        } else {
40
            throw new InvalidArgumentException('Invalid Firstname or Lastname');
41
        }
42
43
        if ($this->emailExists() || !filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
44
            throw new InvalidArgumentException('E-Mail problematic');
45
        } else {
46
            $this->email = htmlspecialchars(strip_tags($this->email));
47
        }
48
49
        if (strlen($this->password)<8 && !preg_match("#[0-9]+#", $this->password) && !preg_match("#[a-zA-Z]+#", $this->password)) {
50
            throw new InvalidArgumentException('Invalid Password');
51
        } else {
52
            $this->password = htmlspecialchars(strip_tags($this->password));
53
        }
54
55
        $stmt->bindParam(':firstname', $this->firstname);
56
        $stmt->bindParam(':lastname', $this->lastname);
57
        $stmt->bindParam(':email', $this->email);
58
59
        $password_hash = password_hash($this->password, PASSWORD_BCRYPT);
60
        $stmt->bindParam(':password', $password_hash);
61
62
        if ($stmt->execute()) {
63
            return true;
64
        }
65
66
        return false;
67
68
    }
69
70
    public function emailExists() {
71
72
        $query = "
73
        SELECT ID, Firstname, Lastname, Email, Password, Language, IsFemale, Birthdate, Height, Aim_Weight, Aim_Date
74
        FROM " . $this->db_table . "
75
        WHERE Email = ?
76
        LIMIT 0,1
77
        ";
78
79
        $this->email = htmlspecialchars(strip_tags($this->email));
80
81
        $stmt = $this->conn->prepare($query);
82
        $stmt->bindParam(1, $this->email);
83
        $stmt->execute();
84
        if ($stmt->rowCount()>0) {
85
86
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
87
            $this->id = $row['ID'];
88
            $this->language = $row['Language'];
89
            $this->firstname = $row['Firstname'];
90
            $this->lastname = $row['Lastname'];
91
            $this->email = $row['Email'];
92
            $this->birthdate = $row['Birthdate'];
93
            $this->isFemale = $row['IsFemale'];
94
            $this->height = $row['Height'];
95
            $this->password = $row['Password'];
96
            $this->aims = array(
97
            "weight" => $row['Aim_Weight'],
98
            "date" => $row['Aim_Date']
99
            );
100
101
            return true;
102
103
        }
104
105
    }
106
107
    public function update() {
108
109
        $query = "
110
        UPDATE " . $this->db_table . " SET
111
        Firstname = :firstname,
112
        Lastname = :lastname,
113
        Language = :language,
114
        IsFemale = :isFemale,
115
        Birthdate = :birthdate,
116
        Height = :height,
117
        Aim_Weight = :aim_weight,
118
        Aim_Date = :aim_date
119
        WHERE ID = :id
120
        ";
121
122
        $stmt = $this->conn->prepare($query);
123
        $this->firstname = htmlspecialchars(strip_tags($this->firstname));
124
        $this->lastname = htmlspecialchars(strip_tags($this->lastname));
125
        $this->language = htmlspecialchars(strip_tags($this->language));
126
        $this->isFemale = htmlspecialchars(strip_tags($this->isFemale));
127
        $this->birthdate = htmlspecialchars(strip_tags($this->birthdate));
128
        $this->height = htmlspecialchars(strip_tags($this->height));
129
        $this->aims->weight = htmlspecialchars(strip_tags($this->aims->weight));
130
        $this->aims->date = htmlspecialchars(strip_tags($this->aims->date));
131
132
        $stmt->bindParam(':firstname', $this->firstname);
133
        $stmt->bindParam(':lastname', $this->lastname);
134
        $stmt->bindParam(':language', $this->language);
135
        $stmt->bindParam(':isFemale', $this->isFemale);
136
        $stmt->bindParam(':height', $this->height);
137
        $stmt->bindParam(':birthdate', $this->birthdate);
138
        $stmt->bindParam(':aim_weight', $this->aims->weight);
139
        $stmt->bindParam(':aim_date', $this->aims->date);
140
        $stmt->bindParam(':id', $this->id);
141
142
        if ($stmt->execute()) {
143
            return true;
144
        }
145
146
        return false;
147
148
    }
149
150
}
151