You are here

class AppWarningsChecker in Apigee Edge 8

A service for handling app warnings.

Hierarchy

Expanded class hierarchy of AppWarningsChecker

1 string reference to 'AppWarningsChecker'
apigee_edge.services.yml in ./apigee_edge.services.yml
apigee_edge.services.yml
1 service uses AppWarningsChecker
apigee_edge.entity.app_warnings_checker in ./apigee_edge.services.yml
Drupal\apigee_edge\Entity\AppWarningsChecker

File

src/Entity/AppWarningsChecker.php, line 32

Namespace

Drupal\apigee_edge\Entity
View source
class AppWarningsChecker implements AppWarningsCheckerInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * AppWarningsChecker constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, TimeInterface $time) {
    $this->entityTypeManager = $entity_type_manager;
    $this->time = $time;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AppWarningsChecker::$entityTypeManager protected property The entity type manager.
AppWarningsChecker::$time protected property The time service.
AppWarningsChecker::getWarnings public function Checks credentials of an app and returns warnings about them. Overrides AppWarningsCheckerInterface::getWarnings
AppWarningsChecker::__construct public function AppWarningsChecker constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.