userRepository = $userRepository; } public function authenticate(string $username, string $password): ?User { $user = $this->userRepository->findByUsername($username); if ($user === null) { return null; } if (!password_verify($password, $user->passwordHash)) { return null; } return $user; } public function createUser(string $username, string $password): User { if ($this->userRepository->findByUsername($username) !== null) { throw new \RuntimeException("User '{$username}' already exists"); } $user = new User(); $user->username = $username; $user->passwordHash = password_hash($password, PASSWORD_ARGON2ID); $user->createdAt = new \DateTimeImmutable(); $user->updatedAt = new \DateTimeImmutable(); $this->userRepository->save($user); return $user; } public function getUserCount(): int { return $this->userRepository->count(); } }