You are here

public function AcquiaLiftAPI::ensureUniqueAgentName in Acquia Lift Connector 7

Same name and namespace in other branches
  1. 7.2 includes/AcquiaLiftAPI.inc \AcquiaLiftAPI::ensureUniqueAgentName()

Returns a unique agent name based on the name passed in.

Checks existing agents in Acquia Lift and adds a suffix if the passed in name already exists. Also ensures the name is within the smaller of Acquia Lift's max length restriction and the passed in max length restriction.

Parameters

$agent_name: The desired agent name.

$max_length: The max length restriction other than that imposed by Acquia Lift itself. The function will use the smaller of the two max length restrictions.

Return value

string A machine-readable name for the agent that does not exist yet in Acquia Lift.

File

includes/acquia_lift.classes.inc, line 1400
Provides an agent type for Acquia Lift

Class

AcquiaLiftAPI
@file Provides an agent type for Acquia Lift

Code

public function ensureUniqueAgentName($agent_name, $max_length) {
  if ($max_length > self::FEATURE_STRING_MAX_LENGTH) {
    $max_length = self::FEATURE_STRING_MAX_LENGTH;
  }
  $agent_name = substr($agent_name, 0, $max_length);
  $existing = $this
    ->getExistingAgents();
  $index = 0;
  $suffix = '';
  while (in_array($agent_name . $suffix, array_keys($existing))) {
    $suffix = '-' . $index;
    while (strlen($agent_name . $suffix) > $max_length) {
      $agent_name = substr($agent_name, 0, -1);
    }
    $index++;
  }
  return $agent_name . $suffix;
}