public function ExistingUser::generateUsername in Acquia Content Hub 8.2
Generate a Username.
Parameters
string $pattern: Pattern to use to generate username.
string[] $pattern_arguments: The arguments to use with the pattern.
Return value
bool|string Username or false if not generated.
Throws
\Exception
1 call to ExistingUser::generateUsername()
- ExistingUser::onParseCdf in src/
EventSubscriber/ Cdf/ ExistingUser.php  - Parses the CDF representation of Content Entities.
 
File
- src/
EventSubscriber/ Cdf/ ExistingUser.php, line 75  
Class
- ExistingUser
 - Prevent user name conflicts.
 
Namespace
Drupal\acquia_contenthub\EventSubscriber\CdfCode
public function generateUsername(string $pattern, string ...$pattern_arguments) {
  // @codingStandardsIgnoreLine
  if (empty($pattern)) {
    throw new \Exception("No pattern could be found for the generated username.");
  }
  $count = 0;
  foreach (self::PATTERN_SPECIFIERS as $specifier) {
    $count += substr_count($pattern, $specifier);
  }
  $arguments_count = count($pattern_arguments);
  if ($count !== $arguments_count) {
    throw new \Exception(sprintf("Mismatched number of pattern arguments to pattern expectations while attempting to generate username. Expected %d; received %d", $count, $arguments_count));
  }
  $username = sprintf($pattern, ...$pattern_arguments);
  if (empty($username)) {
    throw new \Exception("Could not generate a username.");
  }
  $max_length = User::USERNAME_MAX_LENGTH;
  if (strlen($username) > $max_length) {
    return substr($username, 0, $max_length);
  }
  return $username;
}