Skip to content

Commit

Permalink
[apigee#430] Refactor to a trait
Browse files Browse the repository at this point in the history
  • Loading branch information
shadcn committed Jul 20, 2020
1 parent bad7da9 commit 8a50973
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 476 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,13 @@

namespace Drupal\apigee_edge_teams\Form;

use Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface;
use Drupal\apigee_edge\Form\AppCredentialApproveFormBase;
use Drupal\apigee_edge_teams\Entity\Controller\TeamAppCredentialControllerFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides approve confirmation form for team app credential.
*/
class TeamAppCredentialApproveForm extends AppCredentialApproveFormBase {

/**
* The app credential controller factory.
*
* @var \Drupal\apigee_edge_teams\Entity\Controller\TeamAppCredentialControllerFactoryInterface
*/
protected $appCredentialControllerFactory;

/**
* TeamAppDeleteCredentialForm constructor.
*
* @param \Drupal\apigee_edge_teams\Entity\Controller\TeamAppCredentialControllerFactoryInterface $app_credential_controller_factory
* The app credential controller factory.
*/
public function __construct(TeamAppCredentialControllerFactoryInterface $app_credential_controller_factory) {
$this->appCredentialControllerFactory = $app_credential_controller_factory;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('apigee_edge_teams.controller.team_app_credential_controller_factory')
);
}

/**
* {@inheritdoc}
*/
protected function appCredentialController(string $owner, string $app_name): AppCredentialControllerInterface {
return $this->appCredentialControllerFactory->teamAppCredentialController($owner, $app_name);
}

/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return $this->app->toUrl();
}
use TeamAppCredentialFormTrait;

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,13 @@

namespace Drupal\apigee_edge_teams\Form;

use Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface;
use Drupal\apigee_edge\Form\AppCredentialDeleteFormBase;
use Drupal\apigee_edge_teams\Entity\Controller\TeamAppCredentialControllerFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides delete confirmation form for team app credential.
*/
class TeamAppCredentialDeleteForm extends AppCredentialDeleteFormBase {

/**
* The app credential controller factory.
*
* @var \Drupal\apigee_edge_teams\Entity\Controller\TeamAppCredentialControllerFactoryInterface
*/
protected $appCredentialControllerFactory;

/**
* TeamAppDeleteCredentialForm constructor.
*
* @param \Drupal\apigee_edge_teams\Entity\Controller\TeamAppCredentialControllerFactoryInterface $app_credential_controller_factory
* The app credential controller factory.
*/
public function __construct(TeamAppCredentialControllerFactoryInterface $app_credential_controller_factory) {
$this->appCredentialControllerFactory = $app_credential_controller_factory;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('apigee_edge_teams.controller.team_app_credential_controller_factory')
);
}

/**
* {@inheritdoc}
*/
protected function appCredentialController(string $owner, string $app_name): AppCredentialControllerInterface {
return $this->appCredentialControllerFactory->teamAppCredentialController($owner, $app_name);
}

/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return $this->app->toUrl();
}
use TeamAppCredentialFormTrait;

}
43 changes: 43 additions & 0 deletions modules/apigee_edge_teams/src/Form/TeamAppCredentialFormTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Copyright 2020 Google Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

namespace Drupal\apigee_edge_teams\Form;

use Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface;

/**
* Provides form trait for team app credential.
*/
trait TeamAppCredentialFormTrait {

/**
* {@inheritdoc}
*/
protected function appCredentialController(string $owner, string $app_name): AppCredentialControllerInterface {
return \Drupal::service('apigee_edge_teams.controller.team_app_credential_controller_factory')->teamAppCredentialController($owner, $app_name);
}

/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return $this->app->toUrl();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,18 @@
namespace Drupal\apigee_edge_teams\Form;

use Drupal\apigee_edge\Entity\AppInterface;
use Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface;
use Drupal\apigee_edge\Form\AppCredentialGenerateFormBase;
use Drupal\apigee_edge_teams\Entity\Controller\TeamAppCredentialControllerFactoryInterface;
use Drupal\apigee_edge_teams\Entity\Form\TeamAppFormTrait;
use Drupal\apigee_edge_teams\Entity\TeamInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides credential generate form for team app.
*/
class TeamAppCredentialGenerateForm extends AppCredentialGenerateFormBase {

use TeamAppFormTrait;
use TeamAppFormTrait, TeamAppCredentialFormTrait;

/**
* The team from route.
Expand All @@ -43,32 +40,6 @@ class TeamAppCredentialGenerateForm extends AppCredentialGenerateFormBase {
*/
protected $team;

/**
* The app credential controller factory.
*
* @var \Drupal\apigee_edge_teams\Entity\Controller\TeamAppCredentialControllerFactoryInterface
*/
protected $appCredentialControllerFactory;

/**
* TeamAppDeleteCredentialForm constructor.
*
* @param \Drupal\apigee_edge_teams\Entity\Controller\TeamAppCredentialControllerFactoryInterface $app_credential_controller_factory
* The app credential controller factory.
*/
public function __construct(TeamAppCredentialControllerFactoryInterface $app_credential_controller_factory) {
$this->appCredentialControllerFactory = $app_credential_controller_factory;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('apigee_edge_teams.controller.team_app_credential_controller_factory')
);
}

/**
* {@inheritdoc}
*/
Expand All @@ -77,18 +48,11 @@ public function buildForm(array $form, FormStateInterface $form_state, ?AppInter
return parent::buildForm($form, $form_state, $app);
}

/**
* {@inheritdoc}
*/
protected function appCredentialController(string $owner, string $app_name): AppCredentialControllerInterface {
return $this->appCredentialControllerFactory->teamAppCredentialController($owner, $app_name);
}

/**
* {@inheritdoc}
*/
protected function getRedirectUrl(): Url {
return $this->app->toUrl();
return $this->getCancelUrl();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,13 @@

namespace Drupal\apigee_edge_teams\Form;

use Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface;
use Drupal\apigee_edge\Form\AppCredentialRevokeFormBase;
use Drupal\apigee_edge_teams\Entity\Controller\TeamAppCredentialControllerFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides revoke confirmation form for team app credential.
*/
class TeamAppCredentialRevokeForm extends AppCredentialRevokeFormBase {

/**
* The app credential controller factory.
*
* @var \Drupal\apigee_edge_teams\Entity\Controller\TeamAppCredentialControllerFactoryInterface
*/
protected $appCredentialControllerFactory;

/**
* TeamAppDeleteCredentialForm constructor.
*
* @param \Drupal\apigee_edge_teams\Entity\Controller\TeamAppCredentialControllerFactoryInterface $app_credential_controller_factory
* The app credential controller factory.
*/
public function __construct(TeamAppCredentialControllerFactoryInterface $app_credential_controller_factory) {
$this->appCredentialControllerFactory = $app_credential_controller_factory;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('apigee_edge_teams.controller.team_app_credential_controller_factory')
);
}

/**
* {@inheritdoc}
*/
protected function appCredentialController(string $owner, string $app_name): AppCredentialControllerInterface {
return $this->appCredentialControllerFactory->teamAppCredentialController($owner, $app_name);
}

/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return $this->app->toUrl();
}
use TeamAppCredentialFormTrait;

}
49 changes: 1 addition & 48 deletions src/Form/AppCredentialApproveFormBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,13 @@

namespace Drupal\apigee_edge\Form;

use Apigee\Edge\Api\Management\Entity\AppCredential;
use Drupal\apigee_edge\Entity\AppInterface;
use Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Provides approve confirmation base form for app credential.
*/
abstract class AppCredentialApproveFormBase extends ConfirmFormBase {

/**
* The app entity.
*
* @var \Drupal\apigee_edge\Entity\AppInterface
*/
protected $app;

/**
* The consumer key.
*
* @var string
*/
protected $consumerKey;
abstract class AppCredentialApproveFormBase extends AppCredentialConfirmFormBase {

/**
* {@inheritdoc}
Expand Down Expand Up @@ -75,35 +57,6 @@ public function getFormId() {
return 'apigee_edge_app_credential_approve_form';
}

/**
* Returns the app credential controller.
*
* @param string $owner
* The developer id (UUID), email address or team (company) name.
* @param string $app_name
* The name of an app.
*
* @return \Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface
* The app credential controller.
*/
abstract protected function appCredentialController(string $owner, string $app_name) : AppCredentialControllerInterface;

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, ?AppInterface $app = NULL, ?string $consumer_key = NULL) {
// Validate consumer key in app credentials.
if (!in_array($consumer_key, array_map(function (AppCredential $app_credential) {
return $app_credential->getConsumerKey();
}, $app->getCredentials()))) {
throw new NotFoundHttpException();
}

$this->app = $app;
$this->consumerKey = $consumer_key;
return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
Expand Down
Loading

0 comments on commit 8a50973

Please sign in to comment.