You are here

public static function TeamAppFormTrait::appExists in Apigee Edge 8

File

modules/apigee_edge_teams/src/Entity/Form/TeamAppFormTrait.php, line 43

Class

TeamAppFormTrait
Helper trait that contains team app (create/edit) form specific tweaks.

Namespace

Drupal\apigee_edge_teams\Entity\Form

Code

public static function appExists(string $name, array $element, FormStateInterface $form_state) : bool {

  // Do not validate if app name is not set.
  if ($name === '') {
    return FALSE;
  }

  // We use the team app controller factory here instead of entity
  // query to reduce the number API calls. (Entity query may load all
  // developers to return whether the given team has an app with
  // the provided name already.)

  /** @var \Drupal\apigee_edge_teams\Entity\Controller\TeamAppControllerFactoryInterface $factory */
  $factory = \Drupal::service('apigee_edge_teams.controller.team_app_controller_factory');
  $app = TRUE;
  try {
    $app = $factory
      ->teamAppController($form_state
      ->getValue('owner'))
      ->load($name);
  } catch (ApiException $exception) {
    if ($exception instanceof ClientErrorException && $exception
      ->getEdgeErrorCode() === 'developer.service.AppDoesNotExist') {
      $app = FALSE;
    }
    else {

      // Fail safe, return TRUE in case of an API communication error or an
      // unexpected response.
      $context = [
        '%app_name' => $name,
        '%owner' => $form_state
          ->getValue('owner'),
      ];
      $context += Error::decodeException($exception);
      \Drupal::logger('apigee_edge_teams')
        ->error("Unable to properly validate an app name's uniqueness. App name: %app_name. Owner: %owner. @message %function (line %line of %file). <pre>@backtrace_string</pre>", $context);
    }
  }
  return (bool) $app;
}