-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserRepository.php
More file actions
89 lines (74 loc) · 2.2 KB
/
UserRepository.php
File metadata and controls
89 lines (74 loc) · 2.2 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
<?php
require 'DB.php';
require 'User.php';
class UserRepository
{
private $db = null;
public function __construct()
{
$this->db = new DB();
}
public function save($name, $email): ?User
{
try {
$connection = $this->db->getConnection();
$query = 'INSERT INTO users(name, email) VALUES (:name, :email)';
$statement = $connection->prepare($query);
$statement->execute([
'name' => $name,
'email' => $email
]);
return new User($name, $email);
} catch (Exception $exception) {
echo $exception->getMessage();
}
return null;
}
public function listAll(): ?array
{
try {
$connection = $this->db->getConnection();
$query = 'SELECT * FROM users';
$statement = $connection->prepare($query);
$statement->execute();
return $statement->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $exception) {
echo $exception->getMessage();
}
return null;
}
public function exist(string $email): bool
{
try {
$connection = $this->db->getConnection();
$query = 'SELECT COUNT(*) as count FROM users WHERE email = :email';
$statement = $connection->prepare($query);
$statement->execute(
[
'email' => $email
]
);
$result = $statement->fetch();
return $result['count'] > 0;
} catch (Exception $exception) {
echo $exception->getMessage();
}
return true;
}
public function update(string $name, string $email): void
{
try {
$connection = $this->db->getConnection();
$query = 'UPDATE users SET name = :name WHERE email = :email';
$statement = $connection->prepare($query);
$statement->execute(
[
'email' => $email,
'name' => $name
]
);
} catch (Exception $exception) {
echo $exception->getMessage();
}
}
}