User   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 89.74%

Importance

Changes 0
Metric Value
dl 0
loc 125
ccs 35
cts 39
cp 0.8974
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getGravatar() 0 14 3
A verifyPassword() 0 4 1
A delete() 0 9 2
A setPassword() 0 3 1
A getUser() 0 11 3
A getAllUsers() 0 9 1
1
<?php
2
3
namespace Alvo\User;
4
5
use \Anax\Database\ActiveRecordModel;
6
use \Anax\DI\InjectionAwareInterface;
7
use \Anax\DI\InjectionAwareTrait;
8
9
/**
10
 * A database driven model.
11
 */
12
class User extends ActiveRecordModel implements InjectionAwareInterface
13
{
14
    use InjectionAwareTrait;
15
    use UserUtils;
16
17
18
19
    /**
20
     * @var string $tableName name of the database table.
21
     */
22
    protected $tableName = "User";
23
24
    /**
25
     * Columns in the table.
26
     *
27
     * @var integer $id primary key auto incremented.
28
     */
29
    public $id;
30
    public $email;
31
    public $password;
32
    public $created;
33
    public $updated;
34
    public $deleted;
35
    public $admin;
36
37
38
39
    /**
40
     * Set the password.
41
     *
42
     * @param string $password the password to use.
43
     *
44
     * @return void
45
     */
46 1
    public function setPassword($password)
47
    {
48 1
        $this->password = password_hash($password, PASSWORD_DEFAULT);
0 ignored issues
show
Bug introduced by
The constant Alvo\User\PASSWORD_DEFAULT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The function password_hash was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        $this->password = /** @scrutinizer ignore-call */ password_hash($password, PASSWORD_DEFAULT);
Loading history...
49 1
    }
50
51
52
53
    /**
54
     * Verify the email and the password, if successful the object contains
55
     * all details from the database row.
56
     *
57
     * @param string $email  email to check.
58
     * @param string $password the password to use.
59
     *
60
     * @return boolean true if email and password matches, else false.
61
     */
62 1
    public function verifyPassword($email, $password)
63
    {
64 1
        $this->find("email", $email);
65 1
        return password_verify($password, $this->password);
0 ignored issues
show
Bug introduced by
The function password_verify was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        return /** @scrutinizer ignore-call */ password_verify($password, $this->password);
Loading history...
66
    }
67
68
69
70 1
    public function getUser($column = null, $param = null)
71
    {
72 1
        if (!$column) {
73 1
            $column = "id";
74
        }
75
76 1
        if (!$param) {
77 1
            $param = $this->di->get("session")->get("userId");
78
        }
79
80 1
        return $this->find($column, $param);
81
    }
82
83
84
85 1
    public function getAllUsers()
86
    {
87 1
        return $this->db
88 1
            ->connect()
89 1
            ->select()
90 1
            ->from($this->tableName)
91 1
            ->where("deleted is NULL")
92 1
            ->execute()
93 1
            ->fetchAllClass(get_class($this));
94
    }
95
96
97
98 1
    public function delete($id = null)
99
    {
100 1
        $id = $id ?: $this->id;
101
102 1
        $comment = new User();
103 1
        $comment->setDb($this->db);
104 1
        $comment->find("id", $id);
105 1
        $comment->deleted = date("Y-m-d H:i:s");
106 1
        $comment->save();
107 1
    }
108
109
110
111
    /**
112
     * Get either a Gravatar URL or complete image tag for a specified email address.
113
     *
114
     * @param string $email The email address
115
     * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
116
     * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
117
     * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
118
     * @param boole $img True to return a complete IMG tag False for just the URL
0 ignored issues
show
Bug introduced by
The type Alvo\User\boole 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...
119
     * @param array $atts Optional, additional key/value attributes to include in the IMG tag
120
     * @return String containing either just a URL or a complete image tag
121
     * @source https://gravatar.com/site/implement/images/php/
122
     */
123 1
    public function getGravatar($email = null, $sss = 80, $ddd = 'mm', $rrr = 'g', $img = false, $atts = array())
124
    {
125 1
        $email = $email ?? $this->email;
126 1
        $url = 'https://www.gravatar.com/avatar/';
127 1
        $url .= md5(strtolower(trim($email)));
128 1
        $url .= "?s=$sss&d=$ddd&r=$rrr";
129 1
        if ($img) {
130
            $url = '<img src="' . $url . '"';
131
            foreach ($atts as $key => $val) {
132
                $url .= ' ' . $key . '="' . $val . '"';
133
            }
134
            $url .= ' />';
135
        }
136 1
        return $url;
137
    }
138
}
139