Passed
Push — Auth ( a80f44...e088d1 )
by Stone
03:37 queued 55s
created

UserModel::isEmailUsed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Core\Container;
6
use Core\Model;
7
8
class UserModel extends Model
9
{
10
11
    private $userTbl;
12
13
    public function __construct(Container $container)
14
    {
15
        parent::__construct($container);
16
        $this->userTbl = $this->getTablePrefix("users");
17
    }
18
19
    /**
20
     * Get all the data about a user for posts (Author).
21
     * @param int $authorId
22
     * @return array
23
     * @throws \ReflectionException
24
     */
25
    public function getAuthorDetails(int $authorId)
26
    {
27
        return $this->getRowById($authorId);
28
    }
29
30
    /**
31
     * check if the email is present in the database
32
     * @param string $email
33
     * @return bool
34
     * @throws \Exception
35
     */
36
    public function isEmailUsed(string $email)
37
    {
38
        $sql = "
39
            SELECT * FROM $this->userTbl WHERE email = :email
40
        ";
41
        $this->query($sql);
42
        $this->bind(':email', $email);
43
        $this->execute();
44
45
        return $this->stmt->rowCount() > 0;
46
    }
47
48
    public function registerUser(\stdClass $userData) : int
49
    {
50
51
        $passwordHash = password_hash($userData->password, PASSWORD_DEFAULT );
52
53
        $sql = "
54
            INSERT INTO $this->userTbl (username, email, password, surname, name, creation_date, last_update, roles_idroles, locked_out, bad_login_tries)
55
            VALUES (:username, :email, :password, :surname, :name, NOW(), NOW(), :roles_idroles, 1, 0)
56
        ";
57
        $this->query($sql);
58
        $this->bind(':username', $userData->username);
59
        $this->bind(':email', $userData->email);
60
        $this->bind(':password', $passwordHash);
61
        $this->bind(':surname', $userData->surname);
62
        $this->bind(':name', $userData->name);
63
        $this->bind(':roles_idroles', 1);
64
        $this->execute();
65
66
        return (int)$this->dbh->lastInsertId();
67
68
    }
69
}