You are here

class OverrideAppKeysOnGenerate in Apigee Edge 8

Developer app credential generate event subscriber.

Hierarchy

  • class \Drupal\apigee_edge_test_app_keys\EventSubscriber\OverrideAppKeysOnGenerate implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of OverrideAppKeysOnGenerate

1 string reference to 'OverrideAppKeysOnGenerate'
apigee_edge_test_app_keys.services.yml in tests/modules/apigee_edge_test_app_keys/apigee_edge_test_app_keys.services.yml
tests/modules/apigee_edge_test_app_keys/apigee_edge_test_app_keys.services.yml
1 service uses OverrideAppKeysOnGenerate
apigee_edge.event_subscriber.override_app_keys_on_generate in tests/modules/apigee_edge_test_app_keys/apigee_edge_test_app_keys.services.yml
Drupal\apigee_edge_test_app_keys\EventSubscriber\OverrideAppKeysOnGenerate

File

tests/modules/apigee_edge_test_app_keys/src/EventSubscriber/OverrideAppKeysOnGenerate.php, line 33

Namespace

Drupal\apigee_edge_test_app_keys\EventSubscriber
View source
class OverrideAppKeysOnGenerate implements EventSubscriberInterface {

  /**
   * The developer app credential controller factory.
   *
   * @var \Drupal\apigee_edge\Entity\Controller\DeveloperAppCredentialControllerFactoryInterface
   */
  private $devAppCredentialControllerFactory;

  /**
   * OverrideAppKeysOnGenerate constructor.
   *
   * @param \Drupal\apigee_edge\Entity\Controller\DeveloperAppCredentialControllerFactoryInterface $dev_app_credential_controller_factory
   *   The developer app credential controller factory.
   */
  public function __construct(DeveloperAppCredentialControllerFactoryInterface $dev_app_credential_controller_factory) {
    $this->devAppCredentialControllerFactory = $dev_app_credential_controller_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      AppCredentialGenerateEvent::EVENT_NAME => 'overrideAppKeyOnGenerate',
    ];
  }

  /**
   * Overrides auto-generated key for an app.
   *
   * @param \Drupal\apigee_edge\Event\AppCredentialGenerateEvent $event
   *   App credential generation event.
   *
   * @throws \Drupal\Core\TempStore\TempStoreException
   */
  public function overrideAppKeyOnGenerate(AppCredentialGenerateEvent $event) {
    $random = new Random();
    if ($event
      ->getAppType() === AppCredentialGenerateEvent::APP_TYPE_DEVELOPER) {
      $credential_controller = $this->devAppCredentialControllerFactory
        ->developerAppCredentialController($event
        ->getOwnerId(), $event
        ->getAppName());
    }
    else {

      // TODO Finish when Company apps gets supported.
    }
    $prefix = apigee_edge_test_app_keys_get_prefix();
    try {
      $credential_controller
        ->delete($event
        ->getCredential()
        ->getConsumerKey());
      $new_consumer_key = "{$prefix}-{$random->name()}";
      try {
        $credential_controller
          ->create($new_consumer_key, "{$prefix}-{$random->name()}");
        try {
          $products = array_map(function (CredentialProduct $item) {
            return $item
              ->getApiproduct();
          }, $event
            ->getCredential()
            ->getApiProducts());
          $credential_controller
            ->addProducts($new_consumer_key, $products);
        } catch (ApiException $e) {
          watchdog_exception('apigee_edge', $e, 'Unable to assign API products to the newly generated API key on Apigee Edge for @app app. !message', [
            '@app' => "{$event->getOwnerId()}:{$event->getAppName()}",
          ]);
          try {
            $credential_controller
              ->delete($new_consumer_key);
          } catch (ApiException $e) {
            watchdog_exception('apigee_edge', $e, 'Unable to delete newly generated API key after API product re-association has failed on Apigee Edge for @app app. !message', [
              '@app' => "{$event->getOwnerId()}:{$event->getAppName()}",
            ]);
          }
        }
      } catch (ApiException $e) {
        watchdog_exception('apigee_edge', $e, 'Unable to create new API key on Apigee Edge for @app app. !message', [
          '@app' => "{$event->getOwnerId()}:{$event->getAppName()}",
        ]);
      }
    } catch (ApiException $e) {
      watchdog_exception('apigee_edge', $e, 'Unable to delete auto-generated key of @app app on Apigee Edge. !message', [
        '@app' => "{$event->getOwnerId()}:{$event->getAppName()}",
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OverrideAppKeysOnGenerate::$devAppCredentialControllerFactory private property The developer app credential controller factory.
OverrideAppKeysOnGenerate::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
OverrideAppKeysOnGenerate::overrideAppKeyOnGenerate public function Overrides auto-generated key for an app.
OverrideAppKeysOnGenerate::__construct public function OverrideAppKeysOnGenerate constructor.