Skip to content

Commit

Permalink
Better naming for the new API and implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
mxr576 committed Aug 30, 2023
1 parent 1d145ba commit 65ee58d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion apigee_edge.module
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ function apigee_edge_user_cancel_methods_alter(&$methods) {
* Implements hook_user_delete().
*/
function apigee_edge_user_delete(UserInterface $account) {
(\Drupal::service('apigee_edge.user_removal_handler'))($account);
(\Drupal::service('apigee_edge.post_user_removal_handler'))($account);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions apigee_edge.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,6 @@ services:
class: Drupal\apigee_edge\OauthTokenFileStorage
arguments: ['@config.factory', '@file_system', '@logger.channel.apigee_edge']

apigee_edge.user_removal_handler:
class: Drupal\apigee_edge\User\RemoveDeveloperWithUserSynchronousUserRemovalHandler
apigee_edge.post_user_removal_handler:
class: Drupal\apigee_edge\User\SynchronousRelatedDeveloperAccountRemoverPostUserDeleteActionPerformer
arguments: ['@entity_type.manager', '@logger.channel.apigee_edge']
8 changes: 4 additions & 4 deletions modules/apigee_edge_teams/apigee_edge_teams.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ services:
class: Drupal\apigee_edge_teams\CliService
arguments: ['@apigee_edge.apigee_edge_mgmt_cli_service']

apigee_edge_teams.user_removal_handler:
class: Drupal\apigee_edge_teams\User\RemoveTeamRolesWithUserSynchronousUserRemovalHandler
decorates: apigee_edge.user_removal_handler
arguments: [ '@apigee_edge_teams.user_removal_handler.inner', '@entity_type.manager', '@logger.channel.apigee_edge_teams' ]
apigee_edge_teams.post_user_removal_handler:
class: Drupal\apigee_edge_teams\User\SynchronousTeamRolesRemoverPostUserDeleteActionPerformer
decorates: apigee_edge.post_user_removal_handler
arguments: [ '@apigee_edge_teams.post_user_removal_handler.inner', '@entity_type.manager', '@logger.channel.apigee_edge_teams' ]
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace Drupal\apigee_edge_teams\User;

use Drupal\apigee_edge\User\UserRemovalHandlerInterface;
use Drupal\apigee_edge\User\PostUserDeleteActionPerformerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Utility\Error;
use Drupal\user\UserInterface;
Expand All @@ -30,31 +30,31 @@
/**
* Ensures team roles of the removed user also get deleted.
*/
final class RemoveTeamRolesWithUserSynchronousUserRemovalHandler implements UserRemovalHandlerInterface {
final class SynchronousTeamRolesRemoverPostUserDeleteActionPerformer implements PostUserDeleteActionPerformerInterface {

/**
* RemoveTeamRolesWithUserSynchronousUserRemovalHandler constructor.
* Constructs a new object.
*/
public function __construct(private readonly UserRemovalHandlerInterface $decorated, private readonly EntityTypeManagerInterface $entityTypeManager, private readonly LoggerInterface $logger) {}
public function __construct(private readonly PostUserDeleteActionPerformerInterface $decorated, private readonly EntityTypeManagerInterface $entityTypeManager, private readonly LoggerInterface $logger) {}

/**
* {@inheritdoc}
*/
public function __invoke(UserInterface $account): void {
($this->decorated)($account);
public function __invoke(UserInterface $user): void {
($this->decorated)($user);

/** @var \Drupal\apigee_edge_teams\Entity\Storage\TeamMemberRoleStorageInterface $team_member_role_storage */
$team_member_role_storage = $this->entityTypeManager->getStorage('team_member_role');
// When a user gets deleted then its developer account also gets deleted
// from Apigee Edge which removes its (team) company memberships.
// We must delete this user's team roles from Drupal as well.
foreach ($team_member_role_storage->loadByDeveloper($account) as $team_member_roles_in_team) {
foreach ($team_member_role_storage->loadByDeveloper($user) as $team_member_roles_in_team) {
try {
$team_member_roles_in_team->delete();
}
catch (\Exception $e) {
Error::logException($this->logger, $e, "Integrity check: Failed to remove %developer team member's roles in %team team when its Drupal user got deleted. Reason: @message", [
'%developer' => $account->getEmail(),
'%developer' => $user->getEmail(),
'%team' => $team_member_roles_in_team->getTeam()->id(),
], LogLevel::CRITICAL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@
use Drupal\user\UserInterface;

/**
* Contract for triggering an Apigee specific reaction on user removal.
* Contract for triggering Apigee specific reactions _after_ user removal.
*
* It is important to know that when implementations are called the user
* entity is already removed and the database transaction is closed, there
* is no way to roll that back.
*/
interface UserRemovalHandlerInterface {
interface PostUserDeleteActionPerformerInterface {

/**
* React on user removal.
*
* @param \Drupal\user\UserInterface $account
* The deleted user account.
* @param \Drupal\user\UserInterface $user
* The deleted user.
*
* @see \hook_ENTITY_TYPE_delete()
*/
public function __invoke(UserInterface $account): void;
public function __invoke(UserInterface $user): void;

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* @see \apigee_edge_user_delete()
*/
final class RemoveDeveloperWithUserSynchronousUserRemovalHandler implements UserRemovalHandlerInterface {
final class SynchronousRelatedDeveloperAccountRemoverPostUserDeleteActionPerformer implements PostUserDeleteActionPerformerInterface {

/**
* Entity type manager.
Expand All @@ -52,7 +52,7 @@ final class RemoveDeveloperWithUserSynchronousUserRemovalHandler implements User
private LoggerInterface $logger;

/**
* RemoveDeveloperWithUserSynchronousUserRemovalHandler constructor.
* Constructs a new object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager.
Expand All @@ -67,27 +67,27 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager, Log
/**
* {@inheritdoc}
*/
public function __invoke(UserInterface $account): void {
public function __invoke(UserInterface $user): void {
// Do not try to delete developer of the anonymous user because it does
// not exist.
if ($account->isAnonymous()) {
if ($user->isAnonymous()) {
return;
}

try {
/** @var \Drupal\apigee_edge\Entity\DeveloperInterface|null $developer */
$developer = $this->entityTypeManager->getStorage('developer')->load($account->getEmail());
$developer = $this->entityTypeManager->getStorage('developer')->load($user->getEmail());
// Sanity check, the developer may not exist in Apigee Edge.
if ($developer) {
$developer->delete();
$this->logger->info('The @developer developer has been deleted as a reaction to removing its user account.', [
'@developer' => $account->getEmail(),
'@developer' => $user->getEmail(),
]);
}
}
catch (\Exception $exception) {
$context = [
'@developer' => $account->getEmail(),
'@developer' => $user->getEmail(),
];
Error::logException($this->logger, $exception, 'The @developer developer could not be deleted as a reaction to removing its user account. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
}
Expand Down

0 comments on commit 65ee58d

Please sign in to comment.