implement save for update and new users

This commit is contained in:
Yisroel Baum 2025-11-02 09:57:52 +02:00
parent c908381d1e
commit 2c2b1751a7
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -26,5 +26,33 @@ class FlatFileUserRepository implements UserRepository
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']
);
}
}