1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Source\Models; |
4
|
|
|
|
5
|
|
|
use Source\Core\Model; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @package Source\Models |
9
|
|
|
*/ |
10
|
|
|
class User extends Model |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* User constructor. |
14
|
|
|
*/ |
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
parent::__construct("user", ["id"], ["first_name", "last_name", "email", "password"]); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param string $name |
22
|
|
|
* @param string $email |
23
|
|
|
* @param string $password |
24
|
|
|
* @param string|null $document |
25
|
|
|
* @return User |
26
|
|
|
*/ |
27
|
|
|
public function bootstrap( |
28
|
|
|
string $first_name, |
29
|
|
|
string $last_name, |
30
|
|
|
string $email, |
31
|
|
|
string $password |
32
|
|
|
): User { |
33
|
|
|
$this->first_name = $first_name; |
|
|
|
|
34
|
|
|
$this->last_name = $last_name; |
|
|
|
|
35
|
|
|
$this->email = $email; |
|
|
|
|
36
|
|
|
$this->password = $password; |
|
|
|
|
37
|
|
|
$this->cpf = 00000000000; |
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $email |
43
|
|
|
* @param string $columns |
44
|
|
|
* @return null|User |
45
|
|
|
*/ |
46
|
|
|
public function findByEmail(string $email, string $columns = "*"): ?User |
47
|
|
|
{ |
48
|
|
|
$find = $this->find("email = :email", "email={$email}", $columns); |
49
|
|
|
|
50
|
|
|
return $find->fetch(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function updateUser(): bool |
54
|
|
|
{ |
55
|
|
|
$userId = $this->id; |
|
|
|
|
56
|
|
|
|
57
|
|
|
if ($this->find("email = :e AND id != :i", "e={$this->email}&i={$userId}", "id")->fetch()) { |
58
|
|
|
$this->error = "O e-mail informado já está cadastrado"; |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->update($this->safe(), "id = :id", "id={$userId}"); |
63
|
|
|
|
64
|
|
|
if ($this->fail()) { |
65
|
|
|
$this->error = "Erro ao atualizar, verifique os dados"; |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function save(): bool |
76
|
|
|
{ |
77
|
|
|
if (!$this->required()) { |
78
|
|
|
$this->error = "Nome, email e senha são obrigatórios"; |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (!is_email($this->email)) { |
83
|
|
|
$this->error = "O e-mail informado não tem um formato válido"; |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (!is_passwd($this->password)) { |
88
|
|
|
$min = CONF_PASSWD_MIN_LEN; |
89
|
|
|
$max = CONF_PASSWD_MAX_LEN; |
90
|
|
|
$this->error = "A senha deve ter entre {$min} e {$max} caracteres"; |
91
|
|
|
return false; |
92
|
|
|
} else { |
93
|
|
|
$this->password = passwd($this->password); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** User Update */ |
97
|
|
|
if (!empty($this->id)) { |
|
|
|
|
98
|
|
|
$userId = $this->id; |
99
|
|
|
|
100
|
|
|
if ($this->find("email = :e AND id != :i", "e={$this->email}&i={$userId}", "id")->fetch()) { |
101
|
|
|
$this->error = "O e-mail informado já está cadastrado"; |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->update($this->safe(), "id = :id", "id={$userId}"); |
106
|
|
|
if ($this->fail()) { |
107
|
|
|
error_log(($this->fail())->getMessage()); |
108
|
|
|
$this->error = "Erro ao atualizar, verifique os dados"; |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** User Create */ |
114
|
|
|
if (empty($this->id)) { |
115
|
|
|
if ($this->findByEmail($this->email, 'id')) { |
116
|
|
|
$this->error = "O e-mail informado já está cadastrado"; |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$userId = $this->create($this->safe()); |
121
|
|
|
if ($this->fail()) { |
122
|
|
|
error_log(($this->fail())->getMessage()); |
123
|
|
|
$this->error = "Erro ao cadastrar, verifique os dados"; |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->data = ($this->findById($userId))->data(); |
|
|
|
|
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|