You are here

protected function SimpleFbConnectUserManager::generateUniqueUsername in Simple FB Connect 8.2

Same name and namespace in other branches
  1. 8.3 src/SimpleFbConnectUserManager.php \Drupal\simple_fb_connect\SimpleFbConnectUserManager::generateUniqueUsername()

Ensures that Drupal usernames will be unique.

Drupal usernames will be generated so that the user's full name on Facebook will become user's Drupal username. This method will check if the username is already used and appends a number until it finds the first available username.

Parameters

string $fb_name: User's full name on Facebook.

2 calls to SimpleFbConnectUserManager::generateUniqueUsername()
SimpleFbConnectUserManager::createUser in src/SimpleFbConnectUserManager.php
Create a new user account.
TestSimpleFbConnectUserManager::subGenerateUniqueUsername in tests/src/Unit/TestSimpleFbConnectUserManager.php
Public method to help unit testing generateUniqueUsername().

File

src/SimpleFbConnectUserManager.php, line 237
Contains \Drupal\simple_fb_connect\SimpleFbConnectUserManager.

Class

SimpleFbConnectUserManager
Contains all logic that is related to Drupal user management.

Namespace

Drupal\simple_fb_connect

Code

protected function generateUniqueUsername($fb_name) {
  $base = trim($fb_name);
  $i = 1;
  $candidate = $base;
  while ($this
    ->loadUserByProperty('name', $candidate)) {
    $i++;
    $candidate = $base . " " . $i;
  }
  return $candidate;
}