You are here

public function AppWarningsChecker::getWarnings in Apigee Edge 8

Checks credentials of an app and returns warnings about them.

Parameters

\Drupal\apigee_edge\Entity\AppInterface $app: The app entity to be checked.

Return value

array An associative array that contains information about the revoked credentials and revoked or pending API products in a credential.

Overrides AppWarningsCheckerInterface::getWarnings

File

src/Entity/AppWarningsChecker.php, line 66

Class

AppWarningsChecker
A service for handling app warnings.

Namespace

Drupal\apigee_edge\Entity

Code

public function getWarnings(AppInterface $app) : array {
  $warnings = [];
  $warnings['revokedCred'] = FALSE;
  $warnings['revokedOrPendingCredProduct'] = FALSE;
  $warnings['expiredCred'] = FALSE;
  $revoked_credentials = [];
  $args = [
    '@app' => mb_strtolower($app
      ->getEntityType()
      ->getSingularLabel()),
  ];
  foreach ($app
    ->getCredentials() as $credential) {

    // Check for revoked credentials.
    if ($credential
      ->getStatus() === AppCredentialInterface::STATUS_REVOKED) {
      $revoked_credentials[] = $credential;
      continue;
    }

    // Check for expired credentials.
    if (($expired_date = $credential
      ->getExpiresAt()) && $this->time
      ->getRequestTime() - $expired_date
      ->getTimestamp() > 0) {
      $warnings['expiredCred'] = $this
        ->t('At least one of the credentials associated with this @app is expired.', $args);
    }

    // Check status of API products for credential.
    foreach ($credential
      ->getApiProducts() as $cred_product) {
      if ($cred_product
        ->getStatus() == CredentialProduct::STATUS_REVOKED || $cred_product
        ->getStatus() == CredentialProduct::STATUS_PENDING) {
        $args['@api_product'] = $this->entityTypeManager
          ->getDefinition('api_product')
          ->getSingularLabel();
        $args['@status'] = $cred_product
          ->getStatus() == CredentialProduct::STATUS_REVOKED ? $this
          ->t('revoked') : $this
          ->t('pending');
        if (count($app
          ->getCredentials()) === 1) {

          /** @var \Drupal\apigee_edge\Entity\ApiProductInterface $apiProduct */
          $api_product = $this->entityTypeManager
            ->getStorage('api_product')
            ->load($cred_product
            ->getApiproduct());
          $args['%name'] = $api_product
            ->label();
          $warnings['revokedOrPendingCredProduct'] = $this
            ->t('%name @api_product associated with this @app is in @status status.', $args);
        }
        else {
          $warnings['revokedOrPendingCredProduct'] = $this
            ->t('At least one @api_product associated with one of the credentials of this @app is in @status status.', $args);
        }
        break;
      }
    }
  }

  // If all credentials are revoked, show a warning.
  if (count($app
    ->getCredentials()) === count($revoked_credentials)) {
    $warnings['revokedCred'] = $this
      ->t('No valid credentials associated with this @app.', $args);
  }
  return $warnings;
}