function createUser(
array $data,
callable $emailExists,
callable $saveUser
): int {
$email = $data['email'] ?? null;
if ($email === null || trim($email) === '') {
throw new InvalidArgumentException('Email is required');
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('Invalid email format');
}
if ($emailExists($email)) {
throw new DomainException('Email already taken');
}
return $saveUser($data);
}