You are here

protected function AppCreateForm::saveAppCredentials in Apigee Edge 8

Save app credentials on Apigee Edge.

It should log failures but it should not display messages to users. This is handled in save().

Parameters

\Drupal\apigee_edge\Entity\AppInterface $app: The app entity which credentials gets updated.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object with the credential related changes.

Return value

bool|null TRUE on success, FALSE or failure, NULL if no action performed (because credentials did not change).

Overrides AppForm::saveAppCredentials

File

src/Entity/Form/AppCreateForm.php, line 200

Class

AppCreateForm
Base entity form for developer- and team (company) app create forms.

Namespace

Drupal\apigee_edge\Entity\Form

Code

protected function saveAppCredentials(AppInterface $app, FormStateInterface $form_state) : ?bool {

  // On app creation we only support creation of one app credential at this
  // moment.
  $result = FALSE;
  $app_credential_controller = $this
    ->appCredentialController($app
    ->getAppOwner(), $app
    ->getName());
  $logger = $this
    ->logger('apigee_edge');

  /** @var \Apigee\Edge\Api\Management\Entity\AppCredential[] $credentials */
  $credentials = $app
    ->getCredentials();

  /** @var \Apigee\Edge\Api\Management\Entity\AppCredential $credential */
  $credential = reset($credentials);
  $selected_products = array_values(array_filter((array) $form_state
    ->getValue('api_products')));
  try {
    if ($this
      ->appCredentialLifeTime() === 0) {
      $app_credential_controller
        ->addProducts($credential
        ->id(), $selected_products);
    }
    else {
      $app_credential_controller
        ->delete($credential
        ->id());

      // The value of -1 indicates no set expiry. But the value of 0 is not
      // acceptable by the server (InvalidValueForExpiresIn).
      $app_credential_controller
        ->generate($selected_products, $app
        ->getAttributes(), $app
        ->getCallbackUrl(), [], $this
        ->appCredentialLifeTime() * 86400000);
    }
    $result = TRUE;
  } catch (ApiException $exception) {
    $context = [
      '%app_name' => $app
        ->label(),
      '%owner' => $app
        ->getAppOwner(),
      'link' => $app
        ->toLink()
        ->toString(),
    ];
    $context += Error::decodeException($exception);
    $logger
      ->error('Unable to set up app credentials on a created app. App name: %app_name. Owner: %owner. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
    try {

      // Apps without an associated API product should not exist in
      // Apigee Edge because they cause problems.
      $app
        ->delete();
    } catch (EntityStorageException $exception) {
      $context = Error::decodeException($exception) + $context;
      $logger
        ->critical('Unable automatically remove %app_name app owned by %owner after app credential set up has failed meanwhile app creation. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);

      // save() is not going to redirect the user in this case, but.
      $form_state
        ->setRedirectUrl($app
        ->toUrl('collection'));
    }
  }
  return $result;
}