-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserRepo.php
77 lines (68 loc) · 1.98 KB
/
UserRepo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace repos;
use core\exceptions\ValueError;
use core\models\User;
use core\exceptions\EmailExists;
/*
* Репозиторий работы с моделью пользователя
* Слой абстракции между db gateway и контроллером
* Собирает модели из сырых данных
*/
class UserRepo extends StubRepo
{
public function is_email_unique(string $email): bool
{
$user = $this->db->get_user_by_email($email);
return !$user;
}
public function create_user(string $name, string $email, string $password): int
{
if (!$this->is_email_unique($email)) {
throw new EmailExists();
} else {
$user = new User(
$name,
$email,
$password
);
return $this->db->create_user($user);
}
}
public function list_users(): array
{
return $this->db->list_users();
}
public function get_user(int $id = null, string $email = null): User
{
if (!empty($id)) {
$user = $this->db->get_user_by_id($id);
} else if (!empty($email)) {
$user = $this->db->get_user_by_email($email);
} else {
throw new ValueError();
}
return $user;
}
public function auth_user(string $email, string $password): bool
{
return $this->db->check_user($email, $password);
}
public function update_user(int $id, string $name = null, string $email = null, string $password = null): void
{
$user = $this->db->get_user_by_id($id);
if (!empty($name)) {
$user->name = $name;
}
if (!empty($email)) {
$user->email = $email;
}
if (!empty($password)) {
$user->password = $password;
}
$this->db->update_user($user);
}
public function delete_user(int $id): void
{
$this->db->delete_user($id);
}
}