implement save for update and new users
This commit is contained in:
parent
c908381d1e
commit
2c2b1751a7
1 changed files with 28 additions and 0 deletions
|
|
@ -26,5 +26,33 @@ class FlatFileUserRepository implements UserRepository
|
||||||
|
|
||||||
public function save(User $user): User
|
public function save(User $user): User
|
||||||
{
|
{
|
||||||
|
$data = $this->getUsersData();
|
||||||
|
foreach ($data as $jsonUser) {
|
||||||
|
if ($jsonUser['email'] === $user->getEmail()) {
|
||||||
|
$jsonUser['password'] = $user->getPassword();
|
||||||
|
file_put_contents(
|
||||||
|
$this->pathToUserFile,
|
||||||
|
json_encode($data, JSON_PRETTY_PRINT)
|
||||||
|
);
|
||||||
|
return new User(
|
||||||
|
$jsonUser['email'],
|
||||||
|
$jsonUser['password']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$newUser = [
|
||||||
|
'email' => $user->getEmail(),
|
||||||
|
'password' => $user->getPassword(),
|
||||||
|
];
|
||||||
|
$data[] = $newUser;
|
||||||
|
file_put_contents(
|
||||||
|
$this->pathToUserFile,
|
||||||
|
json_encode($data, JSON_PRETTY_PRINT)
|
||||||
|
);
|
||||||
|
return new User(
|
||||||
|
$newUser['email'],
|
||||||
|
$newUser['password']
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue