Skip to content

Commit 199a48f

Browse files
committed
Use User Entity for retriving users and user by email/ID
1 parent 88c6f30 commit 199a48f

File tree

3 files changed

+98
-23
lines changed

3 files changed

+98
-23
lines changed

src/Dal/UserDal.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,43 @@ public static function update(string $userUuid, UserEntity $userEntity): int|str
6767
return false;
6868
}
6969

70-
public static function getById(string $userUuid): ?array
70+
public static function getById(string $userUuid): UserEntity
7171
{
7272
$bindings = ['userUuid' => $userUuid];
7373
$userBean = R::findOne(self::TABLE_NAME, 'user_uuid = :userUuid ', $bindings);
7474

75-
return $userBean?->export();
75+
return (new UserEntity())->unserialize($userBean?->export());
7676
}
7777

78-
public static function getByEmail(string $email): ?array
78+
public static function getByEmail(string $email): UserEntity
7979
{
8080
$bindings = ['email' => $email];
8181

8282
$userBean = R::findOne(self::TABLE_NAME, 'email = :email', $bindings);
8383

84-
return $userBean?->export();
84+
return (new UserEntity())->unserialize($userBean?->export());
8585
}
8686

87-
public static function getAll(): array
87+
public static function getAll(): ?array
8888
{
89-
return R::findAll(self::TABLE_NAME);
89+
$usersBean = R::findAll(self::TABLE_NAME);
90+
$areAnyUsers = $usersBean && count($usersBean);
91+
92+
if (!$areAnyUsers) {
93+
return []; // guard clause approach
94+
}
95+
96+
return array_map(function (object $userBean): array {
97+
$userEntity = (new UserEntity())->unserialize($userBean?->export());
98+
// Retrieve the User entity fields we want to expose to the client
99+
return [
100+
'userUuid' => $userEntity->getUserUuid(),
101+
'first' => $userEntity->getFirstName(),
102+
'last' => $userEntity->getLastName(),
103+
'email' => $userEntity->getEmail(),
104+
'phone' => $userEntity->getPhone(),
105+
'creationDate' => $userEntity->getCreationDate()
106+
];}, $usersBean);
90107
}
91108

92109
public static function remove(string $userUuid): bool

src/Entity/User.php

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
class User
66
{
7-
private string $userUuid;
7+
private int $sequentialId;
8+
private ?string $userUuid = null;
89

910
private ?string $firstName = null;
1011

@@ -16,7 +17,19 @@ class User
1617

1718
private string $password;
1819

19-
private string $creationDate;
20+
private ?string $creationDate = null;
21+
22+
public function setSequentialId(int $sequentialId): self
23+
{
24+
$this->sequentialId = $sequentialId;
25+
26+
return $this;
27+
}
28+
29+
public function getSequentialId(): int
30+
{
31+
return $this->sequentialId;
32+
}
2033

2134
public function setUserUuid(string $userUuid) :self
2235
{
@@ -25,7 +38,7 @@ public function setUserUuid(string $userUuid) :self
2538
return $this;
2639
}
2740

28-
public function getUserUuid(): string
41+
public function getUserUuid(): ?string
2942
{
3043
return $this->userUuid;
3144
}
@@ -97,8 +110,52 @@ public function setCreationDate(string $creationDate): self
97110
return $this;
98111
}
99112

100-
public function getCreationDate(): string
113+
public function getCreationDate(): ?string
101114
{
102115
return $this->creationDate;
103116
}
117+
118+
public function unserialize(?array $data): self
119+
{
120+
if (!empty($data['id'])) {
121+
$this->setSequentialId($data['id']);
122+
}
123+
124+
if (!empty($data['user_uuid'])) {
125+
$this->setUserUuid($data['user_uuid']);
126+
}
127+
128+
if (!empty($data['first_name'])) {
129+
$this->setFirstName($data['first_name']);
130+
}
131+
132+
if (!empty($data['last_name'])) {
133+
$this->setLastName($data['last_name']);
134+
}
135+
136+
if (!empty($data['email'])) {
137+
$this->setEmail($data['email']);
138+
}
139+
140+
if (!empty($data['phone'])) {
141+
$this->setPhone($data['phone']);
142+
}
143+
144+
if (!empty($data['password'])) {
145+
$this->setPassword($data['password']);
146+
}
147+
148+
if (!empty($data['created_date'])) {
149+
$this->setCreationDate($data['created_date']);
150+
}
151+
152+
return $this;
153+
}
154+
155+
// public function serialize(): array
156+
// {
157+
// // optional. we could also only return the properties we want to make it safe
158+
// // (not all properties should indeed be returned)
159+
// return get_object_vars($this);
160+
// }
104161
}

src/Service/User.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public function login(mixed $data): array
2424
if (UserDal::doesEmailExist($data->email)) {
2525
$user = UserDal::getByEmail($data->email);
2626

27-
if ($user && password_verify($data->password, $user['password'])) {
28-
$userName = "{$user['first_name']} {$user['last_name']}";
27+
if ($user->getEmail() && password_verify($data->password, $user->getPassword())) {
28+
$userName = "{$user->getFirstName()} {$user->getLastName()}";
2929

3030
$currentTime = time();
3131
$jwtToken = JWT::encode(
@@ -128,23 +128,24 @@ public function update(mixed $postBody): array|object
128128

129129
public function retrieveAll(): array
130130
{
131-
$users = UserDal::getAll();
132-
133-
return array_map(function (object $user): object {
134-
// Remove unnecessary "id" field
135-
unset($user['id']);
136-
return $user;
137-
}, $users);
131+
return UserDal::getAll();
138132
}
139133

140134
public function retrieve(string $userUuid): array
141135
{
142136
if (v::uuid()->validate($userUuid)) {
143137
if ($user = UserDal::getById($userUuid)) {
144-
// Removing fields we don't want to expose
145-
unset($user['id']);
146-
147-
return $user;
138+
if ($user->getUserUuid()) {
139+
// Retrieve the needed properties we want to expose for the user
140+
return [
141+
'userUuid' => $user->getUserUuid(),
142+
'first' => $user->getFirstName(),
143+
'last' => $user->getLastName(),
144+
'email' => $user->getEmail(),
145+
'phone' => $user->getPhone(),
146+
'creationDate' => $user->getCreationDate()
147+
];
148+
}
148149
}
149150

150151
return [];

0 commit comments

Comments
 (0)