You are here

protected function AppEditForm::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/AppEditForm.php, line 189

Class

AppEditForm
Base entity form for developer- and team (company) app edit forms.

Namespace

Drupal\apigee_edge\Entity\Form

Code

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

  // We do not support creation of multiple app credentials on the add app
  // form at this moment, but it could happen that a user edits an app
  // that has multiple credentials (probably created outside of Drupal).
  // This is the reason why we have to collect and summarize the result
  // of the app credential changes.
  $results = [];
  $config = $this
    ->config('apigee_edge.common_app_settings');

  // If a user can change associated API products on a credential.
  if ($config
    ->get('user_select')) {
    $app_credential_controller = $this
      ->appCredentialController($app
      ->getAppOwner(), $app
      ->getName());

    // $app->getCredentials() always returns the already saved
    // credentials on Apigee Edge.
    // @see \Drupal\apigee_edge\Entity\DeveloperApp::getCredentials()
    foreach ($form_state
      ->getValue('credential', []) as $credential_key => $credential_changes) {
      foreach ($app
        ->getCredentials() as $credential) {
        if ($credential_key === $credential
          ->getConsumerKey()) {
          $original_api_product_ids = [];

          // Cast it to array to be able handle the same way the single- and
          // multi-select configuration.
          $new_api_product_ids = array_filter((array) $credential_changes['api_products']);
          foreach ($credential
            ->getApiProducts() as $original_api_product) {
            $original_api_product_ids[] = $original_api_product
              ->getApiproduct();
          }
          try {
            $product_list_changed = FALSE;

            // Remove API products from the credential.
            if (array_diff($original_api_product_ids, $new_api_product_ids)) {
              foreach (array_diff($original_api_product_ids, $new_api_product_ids) as $api_product_to_remove) {
                $app_credential_controller
                  ->deleteApiProduct($credential_key, $api_product_to_remove);
              }
              $product_list_changed = TRUE;
            }

            // Add new API products to the credential.
            if (array_diff($new_api_product_ids, $original_api_product_ids)) {
              $app_credential_controller
                ->addProducts($credential_key, array_values(array_diff($new_api_product_ids, $original_api_product_ids)));
              $product_list_changed = TRUE;
            }

            // Do not add anything to the results if there were no change.
            if ($product_list_changed) {
              $results[] = TRUE;
            }
            break;
          } catch (ApiException $exception) {
            $results[] = FALSE;
            $context = [
              '%app_name' => $app
                ->label(),
              '%owner' => $app
                ->getAppOwner(),
              'link' => $app
                ->toLink()
                ->toString(),
            ];
            $context += Error::decodeException($exception);
            $this
              ->logger('apigee_edge')
              ->error('Unable to update app credentials on app. App name: %app_name. Owner: %owner. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
          }
        }
      }
    }
  }
  return empty($results) || !in_array(FALSE, $results);
}