|
21 | 21 |
|
22 | 22 | use Apigee\Edge\Api\Management\Entity\AppCredentialInterface; |
23 | 23 | use Apigee\Edge\Structure\CredentialProductInterface; |
24 | | -use Drupal\apigee_edge\Entity\AppInterface; |
25 | | -use Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface; |
26 | 24 | use Drupal\Core\Cache\Cache; |
27 | 25 | use Drupal\Core\Form\FormBase; |
28 | 26 | use Drupal\Core\Form\FormStateInterface; |
29 | 27 | use Drupal\Core\Url; |
| 28 | +use Drupal\apigee_edge\Entity\AppInterface; |
| 29 | +use Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface; |
30 | 30 |
|
31 | 31 | /** |
32 | 32 | * Provides app API key add base form. |
@@ -58,7 +58,7 @@ public function getFormId() { |
58 | 58 | * @return \Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface |
59 | 59 | * The app api-key controller. |
60 | 60 | */ |
61 | | - abstract protected function appCredentialController(string $owner, string $app_name) : AppCredentialControllerInterface; |
| 61 | + abstract protected function appCredentialController(string $owner, string $app_name): AppCredentialControllerInterface; |
62 | 62 |
|
63 | 63 | /** |
64 | 64 | * Returns the redirect url for the app. |
@@ -165,54 +165,75 @@ public function validateForm(array &$form, FormStateInterface $form_state) { |
165 | 165 | * {@inheritdoc} |
166 | 166 | */ |
167 | 167 | public function submitForm(array &$form, FormStateInterface $form_state) { |
| 168 | + $t_args = [ |
| 169 | + '@app' => $this->app->label(), |
| 170 | + ]; |
168 | 171 | $expiry = $form_state->getValue('expiry'); |
169 | 172 | $expiry_date = $form_state->getValue('expiry_date'); |
170 | 173 | $expires_in = $expiry === 'date' ? (strtotime($expiry_date) - time()) * 1000 : -1; |
171 | | - $selected_products = []; |
| 174 | + |
| 175 | + $form_state->setRedirectUrl($this->getRedirectUrl()); |
172 | 176 |
|
173 | 177 | $api_products = $this->getApiProductsForApp($this->app); |
174 | | - if (count($api_products)) { |
175 | | - $selected_products = array_map(function (CredentialProductInterface $api_product) { |
176 | | - return $api_product->getApiproduct(); |
177 | | - }, $api_products); |
| 178 | + // @todo The "Add credential button must not be available when it cannot |
| 179 | + // be used. |
| 180 | + if ($api_products === []) { |
| 181 | + // Is this a skeleton key? |
| 182 | + $this->messenger()->addWarning($this->t('The @app @app_entity_label has no @apis associated.', $t_args + [ |
| 183 | + '@app_entity_label' => $this->app->getEntityType()->getSingularLabel(), |
| 184 | + // @todo DI dependency. |
| 185 | + // phpcs:disable |
| 186 | + '@apis' => \Drupal::entityTypeManager()->getDefinition('api_product')->getPluralLabel(), |
| 187 | + // phpcs:enable |
| 188 | + ])); |
| 189 | + return; |
178 | 190 | } |
179 | 191 |
|
180 | | - $args = [ |
181 | | - '@app' => $this->app->label(), |
182 | | - ]; |
| 192 | + $selected_products = array_map(static function (CredentialProductInterface $api_product) { |
| 193 | + return $api_product->getApiproduct(); |
| 194 | + }, $api_products); |
183 | 195 |
|
184 | 196 | try { |
185 | 197 | $this->appCredentialController($this->app->getAppOwner(), $this->app->getName()) |
186 | | - ->generate($selected_products, $this->app->getAttributes(), $this->app->getCallbackUrl() ?? "", [], $expires_in); |
| 198 | + ->generate($selected_products, $this->app->getAttributes(), $this->app->getCallbackUrl() ?? '', [], $expires_in); |
187 | 199 | Cache::invalidateTags($this->app->getCacheTags()); |
188 | | - $this->messenger()->addStatus($this->t('New API key added to @app.', $args)); |
189 | | - $form_state->setRedirectUrl($this->getRedirectUrl()); |
| 200 | + $this->messenger()->addStatus($this->t('New API key added to @app.', $t_args)); |
190 | 201 | } |
191 | 202 | catch (\Exception $exception) { |
192 | | - $this->messenger()->addError($this->t('Failed to add API key for @app.', $args)); |
| 203 | + $this->messenger()->addError($this->t('Failed to add API key for @app.', $t_args)); |
193 | 204 | } |
194 | 205 | } |
195 | 206 |
|
196 | 207 | /** |
197 | 208 | * Helper to find API products based on the recently active API key. |
198 | 209 | * |
| 210 | + * Returns the most recent approved credential, if there is any, otherwise |
| 211 | + * returns the most recent revoked credential. |
| 212 | + * |
199 | 213 | * @param \Drupal\apigee_edge\Entity\AppInterface $app |
200 | 214 | * The app entity. |
201 | 215 | * |
202 | | - * @return \Apigee\Edge\Structure\CredentialProductInterface[]|array |
203 | | - * An array of API products. |
| 216 | + * @return \Apigee\Edge\Structure\CredentialProductInterface[] |
| 217 | + * An array of credential API products. |
204 | 218 | */ |
205 | 219 | protected function getApiProductsForApp(AppInterface $app): array { |
206 | | - $approved_credentials = array_filter($app->getCredentials(), function (AppCredentialInterface $credential) { |
207 | | - return $credential->getStatus() === AppCredentialInterface::STATUS_APPROVED; |
208 | | - }); |
| 220 | + if ($app->getCredentials() === []) { |
| 221 | + // Is this a skeleton key? |
| 222 | + return []; |
| 223 | + } |
209 | 224 |
|
210 | | - // Find the recently active one. |
211 | | - usort($approved_credentials, function (AppCredentialInterface $a, AppCredentialInterface $b) { |
| 225 | + $credentials = $app->getCredentials(); |
| 226 | + usort($credentials, static function (AppCredentialInterface $a, AppCredentialInterface $b) { |
212 | 227 | return $b->getIssuedAt() <=> $a->getIssuedAt(); |
213 | 228 | }); |
214 | 229 |
|
215 | | - return count($approved_credentials) ? $approved_credentials[0]->getApiProducts() : []; |
| 230 | + $approved_credentials = array_filter($app->getCredentials(), static function (AppCredentialInterface $credential) { |
| 231 | + return $credential->getStatus() === AppCredentialInterface::STATUS_APPROVED; |
| 232 | + }); |
| 233 | + |
| 234 | + $credential = $approved_credentials !== [] ? reset($approved_credentials) : reset($credentials); |
| 235 | + |
| 236 | + return $credential->getApiProducts(); |
216 | 237 | } |
217 | 238 |
|
218 | 239 | } |
0 commit comments