User::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Oenstrom\User;
4
5
use \Anax\Database\ActiveRecordModel;
6
7
/**
8
 * A database driven model.
9
 */
10
class User extends ActiveRecordModel
11
{
12
    /**
13
     * @var string $tableName name of the database table.
14
     */
15
    protected $tableName = "User";
16
17
18
19
    /**
20
     * Columns in the table.
21
     *
22
     * @var integer $id primary key auto incremented.
23
     * @var string $role not null default "user".
24
     * @var string $username unique not null.
25
     * @var string $password not null.
26
     * @var string $email unique not null.
27
     * @var timestamp $created default CURRENT_TIMESTAMP.
28
     * @var timestamp $deleted default null.
29
     */
30
    public $id;
31
    public $role;
32
    public $username;
33
    public $password;
34
    public $email;
35
    public $created;
36
    //public $updated;
37
    public $deleted;
38
    //public $active;
39
40
41
42
    /**
43
     * Set the name of the user table.
44
     *
45
     * @param string $tableName the name of the user table
46
     */
47 20
    public function __construct($tableName = "User")
48
    {
49 20
        $this->tableName = $tableName;
50 20
    }
51
52
53
54
    /**
55
     * Set the user password.
56
     *
57
     * @param string $password the password to hash.
58
     *
59
     * @return void
60
     */
61 1
    public function setPassword($password)
62
    {
63 1
        $this->password = password_hash($password, PASSWORD_DEFAULT);
64 1
    }
65
66
67
68
    /**
69
     * Check if password is correct.
70
     *
71
     * @param string $username the username of the user
72
     * @param string $password the password of the user
73
     *
74
     * @return boolean true if the password match the user password, else false
75
     */
76 2
    public function verifyPassword($username, $password)
77
    {
78 2
        $this->find("username", $username);
79 2
        return password_verify($password, $this->password);
80
    }
81
82
83
84
    /**
85
     * Check if the username already exists in the database.
86
     *
87
     * @return string|null the username if it exists, else null
88
     */
89 2
    public function usernameExists($username)
90
    {
91 2
        $this->find("username", $username);
92 2
        return $this->username;
93
    }
94
95
96
97
    /**
98
     * Check if the email already exists in the database.
99
     *
100
     * @return string|null the email if it exists, else null
101
     */
102 2
    public function emailExists($email)
103
    {
104 2
        $this->find("email", $email);
105 2
        return $this->email;
106
    }
107
108
109
110
    /**
111
     * Check if the user is an admin.
112
     *
113
     * @return boolean true if user is admin, else false
114
     */
115 1
    public function isAdmin()
116
    {
117 1
        return $this->role === "admin";
118
    }
119
120
121
122
    /**
123
     * Get either a Gravatar URL or complete image tag for the user email.
124
     *
125
     * @param boole $img True to return a complete IMG tag False for just the URL
126
     * @param string $size Size in pixels, defaults to 80px [ 1 - 2048 ]
127
     * @param string $default Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
128
     * @param string $rating Maximum rating (inclusive) [ g | pg | r | x ]
129
     * @param array $atts Optional, additional key/value attributes to include in the IMG tag
130
     * @return String containing either just a URL or a complete image tag
131
     * @source https://gravatar.com/site/implement/images/php/
132
     */
133 1
    public function getGravatar($img = false, $size = 80, $default = 'mm', $rating = 'g', $atts = array())
134
    {
135 1
        $url = 'https://www.gravatar.com/avatar/';
136 1
        $url .= md5(strtolower(trim($this->email)));
137 1
        $url .= "?s=$size&d=$default&r=$rating";
138 1 View Code Duplication
        if ($img) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139 1
            $url = '<img src="' . $url . '" alt="' . $this->email . '"';
140 1
            foreach ($atts as $key => $val) {
141 1
                $url .= ' ' . $key . '="' . $val . '"';
142 1
            }
143 1
            $url .= ' />';
144 1
        }
145 1
        return $url;
146
    }
147
}
148