-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserSQLiteRepository.php
More file actions
132 lines (111 loc) · 4.36 KB
/
UserSQLiteRepository.php
File metadata and controls
132 lines (111 loc) · 4.36 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
declare(strict_types=1);
namespace Infrastructure\Repository;
use Domain\Entities\EntityInterface;
use Domain\Entities\User;
final class UserSQLiteRepository implements UserRepositoryInterface
{
private \PDO $database;
public function __construct()
{
$this->database = new \PDO('sqlite:' . __DIR__ . '/../../../database/app.db');
$this->database->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
public function get(int|string $ID): ?EntityInterface
{
$query = "SELECT id, email, nickname, password, created_at, updated_at FROM users WHERE id = ? LIMIT 1";
$stmt = $this->database->prepare($query);
$stmt->execute([$ID]);
$entity = $stmt->fetchAll(\PDO::FETCH_ASSOC);
if (\count($entity) === 0) {
return null;
}
$entity = (object) $entity[0];
$user = new User(
$entity->id,
$entity->email,
$entity->nickname,
$entity->password,
new \DateTimeImmutable($entity->created_at),
new \DateTimeImmutable($entity->updated_at)
);
$user->setPasswordHash($entity->password);
return $user;
}
public function find(array $filter = []): array
{
$where = [];
$binds = [];
foreach ($filter as $key => $value) {
$where[] = "$key = :$key";
$binds[":$key"] = $value;
}
$where = ' WHERE ' . implode(' and ', $where);
if (count($filter) === 0) {
$where = '';
}
$query = "SELECT id, email, nickname, password, created_at, updated_at FROM users $where";
$stmt = $this->database->prepare($query);
$stmt->execute($binds);
$entities = $stmt->fetchAll(\PDO::FETCH_OBJ);
if (\count($entities) === 0) {
return [];
}
$users = [];
foreach ($entities as $entity) {
$user = new User(
$entity->id,
$entity->email,
$entity->nickname,
$entity->password,
new \DateTimeImmutable($entity->created_at),
new \DateTimeImmutable($entity->updated_at)
);
$user->setPasswordHash($entity->password);
$users[] = $user;
}
return $users;
}
public function create(EntityInterface $entity): bool
{
$query = "INSERT INTO users (id, email, nickname, password, created_at, updated_at)
VALUES (:id, :email, :nickname, :password, :created_at, :updated_at)";
$stmt = $this->database->prepare($query);
$ID = $entity->getID();
$email = $entity->getEmail();
$nickname = $entity->getNickname();
$createdAt = ($entity->getCreatedAt())->format("Y-m-d H:i:s");
$updatedAt = ($entity->getUpdatedAt())->format("Y-m-d H:i:s");
$password = $entity->getPassword();
$stmt->bindParam(':id', $ID, \PDO::PARAM_STR);
$stmt->bindParam(':email', $email, \PDO::PARAM_STR);
$stmt->bindParam(':nickname', $nickname, \PDO::PARAM_STR);
$stmt->bindParam(':password', $password, \PDO::PARAM_STR);
$stmt->bindParam(':created_at', $createdAt , \PDO::PARAM_STR);
$stmt->bindParam(':updated_at', $updatedAt, \PDO::PARAM_STR);
return $stmt->execute();
}
public function update(EntityInterface $entity): bool
{
$query = "UPDATE users SET email = :email, nickname = :nickname, password = :password, updated_at = :updated_at
WHERE id = :id";
$stmt = $this->database->prepare($query);
$ID = $entity->getID();
$email = $entity->getEmail();
$nickname = $entity->getNickname();
$updatedAt = ($entity->getUpdatedAt())->format("Y-m-d H:i:s");
$password = $entity->getPassword();
$stmt->bindParam(':id', $ID, \PDO::PARAM_STR);
$stmt->bindParam(':email', $email, \PDO::PARAM_STR);
$stmt->bindParam(':nickname', $nickname, \PDO::PARAM_STR);
$stmt->bindParam(':password', $password, \PDO::PARAM_STR);
$stmt->bindParam(':updated_at', $updatedAt, \PDO::PARAM_STR);
return $stmt->execute();
}
public function delete(int|string $ID): bool
{
$query = "DELETE FROM users WHERE id = :id";
$stmt = $this->database->prepare($query);
return $stmt->execute([$ID]);
}
}