AppWarningsChecker.php in Apigee Edge 8
File
src/Entity/AppWarningsChecker.php
View source
<?php
namespace Drupal\apigee_edge\Entity;
use Apigee\Edge\Api\Management\Entity\AppCredentialInterface;
use Apigee\Edge\Structure\CredentialProduct;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class AppWarningsChecker implements AppWarningsCheckerInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $time;
public function __construct(EntityTypeManagerInterface $entity_type_manager, TimeInterface $time) {
$this->entityTypeManager = $entity_type_manager;
$this->time = $time;
}
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) {
if ($credential
->getStatus() === AppCredentialInterface::STATUS_REVOKED) {
$revoked_credentials[] = $credential;
continue;
}
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);
}
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) {
$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 (count($app
->getCredentials()) === count($revoked_credentials)) {
$warnings['revokedCred'] = $this
->t('No valid credentials associated with this @app.', $args);
}
return $warnings;
}
}