View source
<?php
namespace Drupal\apigee_edge\Form;
use Apigee\Edge\Api\Management\Entity\AppCredentialInterface;
use Apigee\Edge\Structure\CredentialProductInterface;
use Drupal\apigee_edge\Entity\AppInterface;
use Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
abstract class AppApiKeyAddFormBase extends FormBase {
protected $app;
public function getFormId() {
return 'apigee_edge_app_api_key_add_form';
}
protected abstract function appCredentialController(string $owner, string $app_name) : AppCredentialControllerInterface;
protected abstract function getRedirectUrl() : Url;
protected abstract function apiProductList(array $form, FormStateInterface $form_state) : array;
protected abstract function getAppOwner() : string;
public function buildForm(array $form, FormStateInterface $form_state, ?AppInterface $app = NULL) {
$this->app = $app;
$form['owner'] = [
'#type' => 'value',
'#value' => $this
->getAppOwner(),
];
$form['message'] = [
'#type' => 'html_tag',
'#tag' => 'h3',
'#value' => $this
->t('Do you really want to create a new API key for this @entity_type?', [
'@entity_type' => mb_strtolower($app
->getEntityType()
->getSingularLabel()),
]),
];
$form['expiry'] = [
'#type' => 'select',
'#title' => $this
->t('Set an expiry date'),
'#required' => TRUE,
'#options' => [
'never' => $this
->t('Never'),
'date' => $this
->t('Date'),
],
'#default_value' => 'never',
];
$form['expiry_date'] = [
'#type' => 'date',
'#title' => $this
->t('Select date'),
'#states' => [
'visible' => [
':input[name="expiry"]' => [
'value' => 'date',
],
],
],
];
$form['actions'] = [
'#type' => 'actions',
'cancel' => [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#attributes' => [
'class' => [
'button',
],
],
'#url' => $this
->getRedirectUrl(),
],
'submit' => [
'#type' => 'submit',
'#value' => $this
->t('Confirm'),
'#button_type' => 'primary',
],
];
$form['#attached']['library'][] = 'apigee_edge/apigee_edge.components';
$form['#attributes']['class'][] = 'apigee-edge--form';
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$expiry = $form_state
->getValue('expiry');
$expiry_date = $form_state
->getValue('expiry_date');
if ($expiry === 'date') {
if ((new \DateTimeImmutable($expiry_date))
->diff(new \DateTimeImmutable())->invert !== 1) {
$form_state
->setError($form['expiry_date'], $this
->t('The expiration date must be a future date.'));
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$expiry = $form_state
->getValue('expiry');
$expiry_date = $form_state
->getValue('expiry_date');
$expires_in = $expiry === 'date' ? (strtotime($expiry_date) - time()) * 1000 : -1;
$selected_products = [];
$api_products = $this
->getApiProductsForApp($this->app);
if (count($api_products)) {
$selected_products = array_map(function (CredentialProductInterface $api_product) {
return $api_product
->getApiproduct();
}, $api_products);
}
$args = [
'@app' => $this->app
->label(),
];
try {
$this
->appCredentialController($this->app
->getAppOwner(), $this->app
->getName())
->generate($selected_products, $this->app
->getAttributes(), $this->app
->getCallbackUrl() ?? "", [], $expires_in);
Cache::invalidateTags($this->app
->getCacheTags());
$this
->messenger()
->addStatus($this
->t('New API key added to @app.', $args));
$form_state
->setRedirectUrl($this
->getRedirectUrl());
} catch (\Exception $exception) {
$this
->messenger()
->addError($this
->t('Failed to add API key for @app.', $args));
}
}
protected function getApiProductsForApp(AppInterface $app) : array {
$approved_credentials = array_filter($app
->getCredentials(), function (AppCredentialInterface $credential) {
return $credential
->getStatus() === AppCredentialInterface::STATUS_APPROVED;
});
usort($approved_credentials, function (AppCredentialInterface $a, AppCredentialInterface $b) {
return $a
->getIssuedAt() < $b
->getIssuedAt();
});
return count($approved_credentials) ? $approved_credentials[0]
->getApiProducts() : [];
}
}