Working state

This commit is contained in:
julien
2026-03-16 13:40:18 +01:00
parent dec76fa2c7
commit 557360dfde
57 changed files with 1044 additions and 1668 deletions

View File

@@ -4,9 +4,11 @@ declare(strict_types=1);
namespace App\User;
use App\Shared\Exception\NotFoundException;
use App\Shared\Pagination\PaginatedResult;
use App\User\Exception\DuplicateEmailException;
use App\User\Exception\DuplicateUsernameException;
use App\User\Exception\InvalidRoleException;
use App\User\Exception\RoleAssignmentNotAllowedException;
use App\User\Exception\WeakPasswordException;
final class UserService implements UserServiceInterface
@@ -21,6 +23,23 @@ final class UserService implements UserServiceInterface
return $this->userRepository->findAll();
}
/**
* @return PaginatedResult<User>
*/
public function findPaginated(int $page, int $perPage): PaginatedResult
{
$page = max(1, $page);
$total = $this->userRepository->countAll();
$offset = ($page - 1) * $perPage;
return new PaginatedResult(
$this->userRepository->findPage($perPage, $offset),
$total,
$page,
$perPage,
);
}
public function findById(int $id): ?User
{
return $this->userRepository->findById($id);
@@ -34,7 +53,7 @@ final class UserService implements UserServiceInterface
public function updateRole(int $id, string $role): void
{
$this->assertValidRole($role);
$this->assertRoleCanBeAssigned($role);
$this->requireExistingUser($id);
$this->userRepository->updateRole($id, $role);
}
@@ -45,7 +64,7 @@ final class UserService implements UserServiceInterface
$email = mb_strtolower(trim($email));
$plainPassword = trim($plainPassword);
$this->assertValidRole($role);
$this->assertRoleCanBeAssigned($role);
if ($this->userRepository->findByUsername($username)) {
throw new DuplicateUsernameException($username);
@@ -67,13 +86,15 @@ final class UserService implements UserServiceInterface
return $user;
}
private function assertValidRole(string $role): void
private function assertRoleCanBeAssigned(string $role): void
{
$validRoles = [User::ROLE_USER, User::ROLE_EDITOR, User::ROLE_ADMIN];
if (!in_array($role, $validRoles, true)) {
if (!in_array($role, User::allRoles(), true)) {
throw new InvalidRoleException($role);
}
if (!in_array($role, User::assignableRoles(), true)) {
throw new RoleAssignmentNotAllowedException($role);
}
}
private function requireExistingUser(int $id): User