FreightOrders/src/User/FlatFileUserRepository.php

59 lines
1.6 KiB
PHP

<?php
namespace FreightQuote\User;
class FlatFileUserRepository implements UserRepository
{
private string $pathToUserFile = __DIR__.'/../../storage/users.json';
private function getUsersData(): array
{
$json = file_get_contents($this->pathToUserFile);
$data = json_decode($json, true);
return $data;
}
public function findByEmail(string $email): ?User
{
$data = $this->getUsersData();
foreach ($data as $user) {
if ($user['email'] === $email) {
return new User($user['email'], $user['password']);
}
}
return null;
}
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']
);
}
}