You are here

public function AppApiKeyAddFormBase::submitForm in Apigee Edge 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/AppApiKeyAddFormBase.php, line 167

Class

AppApiKeyAddFormBase
Provides app API key add base form.

Namespace

Drupal\apigee_edge\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $expiry = $form_state
    ->getValue('expiry');
  $expiry_date = $form_state
    ->getValue('expiry_date');
  $expires_in = $expiry === 'date' ? (strtotime($expiry_date) - time()) * 1000 : -1;
  $selected_products = [];
  $api_products = $this
    ->getApiProductsForApp($this->app);
  if (count($api_products)) {
    $selected_products = array_map(function (CredentialProductInterface $api_product) {
      return $api_product
        ->getApiproduct();
    }, $api_products);
  }
  $args = [
    '@app' => $this->app
      ->label(),
  ];
  try {
    $this
      ->appCredentialController($this->app
      ->getAppOwner(), $this->app
      ->getName())
      ->generate($selected_products, $this->app
      ->getAttributes(), $this->app
      ->getCallbackUrl() ?? "", [], $expires_in);
    Cache::invalidateTags($this->app
      ->getCacheTags());
    $this
      ->messenger()
      ->addStatus($this
      ->t('New API key added to @app.', $args));
    $form_state
      ->setRedirectUrl($this
      ->getRedirectUrl());
  } catch (\Exception $exception) {
    $this
      ->messenger()
      ->addError($this
      ->t('Failed to add API key for @app.', $args));
  }
}