You are here

apigee_edge_test_app_keys.module in Apigee Edge 8

Module file for Apigee Edge Test: App keys.

File

tests/modules/apigee_edge_test_app_keys/apigee_edge_test_app_keys.module
View source
<?php

/**
 * @file
 * Module file for Apigee Edge Test: App keys.
 */
use Apigee\Edge\Exception\ApiException;
use Drupal\Component\Utility\Random;
use Drupal\Core\Entity\EntityInterface;

/**
 * Implements hook_entity_insert().
 *
 * TODO Add company apps support later.
 */
function apigee_edge_test_app_keys_developer_app_insert(EntityInterface $entity) {

  /** @var \Drupal\apigee_edge\Entity\DeveloperAppInterface $entity */
  $credentials = $entity
    ->getCredentials();
  $credential = reset($credentials);
  $credential_lifetime = Drupal::config('apigee_edge.developer_app_settings')
    ->get('credential_lifetime');

  // We only care about auto-generated key by Apigee Edge when a new app is
  // being created. We also ignore auto-generated keys if credential lifetime
  // is set, because those get removed automatically and we should not create
  // two new keys for a new app (on the 3rd party service).
  // @see \Drupal\apigee_edge\Entity\Form\DeveloperAppCreateForm::save()
  if ($credential
    ->getExpiresAt() === NULL && $credential_lifetime === 0) {
    $random = new Random();

    /** @var \Drupal\apigee_edge\Entity\Controller\DeveloperAppCredentialControllerFactoryInterface $factory */
    $factory = \Drupal::service('apigee_edge.controller.developer_app_credential_factory');
    $dacc = $factory
      ->developerAppCredentialController($entity
      ->getDeveloperId(), $entity
      ->getName());
    try {
      $dacc
        ->delete($credential
        ->getConsumerKey());
      try {
        $prefix = apigee_edge_test_app_keys_get_prefix();
        $dacc
          ->create("{$prefix}-{$random->name()}", "{$prefix}-{$random->name()}");
      } catch (ApiException $e) {
        watchdog_exception('apigee_edge', $e, 'Unable to create new API key on Apigee Edge for @app app. !message', [
          '@app' => $entity
            ->id(),
        ]);
      }
    } catch (ApiException $e) {
      watchdog_exception('apigee_edge', $e, 'Unable to delete auto-generated key of @app app on Apigee Edge. !message', [
        '@app' => $entity
          ->id(),
      ]);
    }
  }
}

/**
 * Returns the key and secret prefix used by this module.
 *
 * @return string
 *   The prefix.
 */
function apigee_edge_test_app_keys_get_prefix() {
  return 'apigee_edge_test_app_keys';
}

Functions

Namesort descending Description
apigee_edge_test_app_keys_developer_app_insert Implements hook_entity_insert().
apigee_edge_test_app_keys_get_prefix Returns the key and secret prefix used by this module.