Refatoring : Working state

This commit is contained in:
julien
2026-03-16 14:21:31 +01:00
parent d0761ff010
commit d832d598fe
10 changed files with 449 additions and 358 deletions

View File

@@ -3,108 +3,12 @@ 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;
use App\User\Application\UserApplicationService;
final class UserService implements UserServiceInterface
/**
* Pont de compatibilité : l'implémentation métier principale vit désormais dans
* App\User\Application\UserApplicationService.
*/
final class UserService extends UserApplicationService implements UserServiceInterface
{
public function __construct(
private readonly UserRepositoryInterface $userRepository,
) {
}
public function findAll(): array
{
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);
}
public function delete(int $id): void
{
$this->requireExistingUser($id);
$this->userRepository->delete($id);
}
public function updateRole(int $id, string $role): void
{
$this->assertRoleCanBeAssigned($role);
$this->requireExistingUser($id);
$this->userRepository->updateRole($id, $role);
}
public function createUser(string $username, string $email, string $plainPassword, string $role = User::ROLE_USER): User
{
$username = mb_strtolower(trim($username));
$email = mb_strtolower(trim($email));
$plainPassword = trim($plainPassword);
$this->assertRoleCanBeAssigned($role);
if ($this->userRepository->findByUsername($username)) {
throw new DuplicateUsernameException($username);
}
if ($this->userRepository->findByEmail($email)) {
throw new DuplicateEmailException($email);
}
if (mb_strlen($plainPassword) < 8) {
throw new WeakPasswordException();
}
$passwordHash = password_hash($plainPassword, PASSWORD_BCRYPT, ['cost' => 12]);
$user = new User(0, $username, $email, $passwordHash, $role);
$this->userRepository->create($user);
return $user;
}
private function assertRoleCanBeAssigned(string $role): void
{
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
{
$user = $this->userRepository->findById($id);
if ($user === null) {
throw new NotFoundException('Utilisateur', $id);
}
return $user;
}
}