DBAuth   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 16
c 0
b 0
f 0
dl 0
loc 50
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A login() 0 11 4
A logged() 0 3 1
A __construct() 0 4 1
A getUserId() 0 6 2
1
<?php
2
3
namespace Core\Services\Auth;
4
5
use PDOException;
6
use Core\Services\Globals\Globals;
7
use Core\Services\Database\MysqlDatabase;
8
9
class DBAuth
10
{
11
    private $db;
12
    protected $globals;
13
14
    public function __construct(MysqlDatabase $db)
15
    {
16
        $this->db = $db;
17
        $this->globals = new Globals;
18
    }
19
20
    /**
21
     * Returns the Id of the connected User
22
     * @return mixed $id|false
23
     */
24
    public function getUserId()
25
    {
26
        if ($this->logged()) {
27
            return $this->SESSION["auth"];
0 ignored issues
show
Bug Best Practice introduced by
The property SESSION does not exist on Core\Services\Auth\DBAuth. Did you maybe forget to declare it?
Loading history...
28
        }
29
        return \false;
30
    }
31
32
    /**
33
     * Check the login
34
     * @param string $usernameOrEmail
35
     * @param string $password
36
     * @return bool
37
     * @throws PDOException
38
     */
39
    public function login(string $usernameOrEmail, string $password): bool
40
    {
41
        $user = $this->db->prepare("SELECT * FROM user WHERE username = ? or email = ?", [$usernameOrEmail, $usernameOrEmail], \null, \true);
42
        if ($user) {
43
            if (\password_verify($password, $user->password)) {
44
                $_SESSION["auth"] = $user->id;
45
                isset($user->admin) ? $_SESSION['admin'] = $user->admin : \false;
46
                return \true;
47
            }
48
        }
49
        return \false;
50
    }
51
52
    /**
53
     * Checks if the Visitor is logged
54
     * @return bool
55
     */
56
    public function logged(): bool
57
    {
58
        return !empty($this->globals->getSESSION("auth"));
59
    }
60
}
61