From eca9d9c16458306de66386b953a35b80923aa3f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dezs=C5=91=20BICZ=C3=93?= Date: Wed, 30 Aug 2023 11:41:32 +0200 Subject: [PATCH] Better naming for the new API and implementations --- apigee_edge.module | 2 +- apigee_edge.services.yml | 4 ++-- .../apigee_edge_teams.services.yml | 8 ++++---- ...SynchronousPostUserDeleteActionPerformer.php} | 16 ++++++++-------- ...> PostUserDeleteActionPerformerInterface.php} | 14 +++++++++----- ...SynchronousPostUserDeleteActionPerformer.php} | 14 +++++++------- 6 files changed, 31 insertions(+), 27 deletions(-) rename modules/apigee_edge_teams/src/User/{RemoveTeamRolesWithUserSynchronousUserRemovalHandler.php => RemoveTeamRolesOfUserSynchronousPostUserDeleteActionPerformer.php} (73%) rename src/User/{UserRemovalHandlerInterface.php => PostUserDeleteActionPerformerInterface.php} (66%) rename src/User/{RemoveDeveloperWithUserSynchronousUserRemovalHandler.php => RemoveRelatedDeveloperAccountSynchronousPostUserDeleteActionPerformer.php} (87%) diff --git a/apigee_edge.module b/apigee_edge.module index e0fd0e41..304f9021 100644 --- a/apigee_edge.module +++ b/apigee_edge.module @@ -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_delete_action_performer'))($account); } /** diff --git a/apigee_edge.services.yml b/apigee_edge.services.yml index f09bc2bb..82334a68 100644 --- a/apigee_edge.services.yml +++ b/apigee_edge.services.yml @@ -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_delete_action_performer: + class: Drupal\apigee_edge\User\RemoveRelatedDeveloperAccountSynchronousPostUserDeleteActionPerformer arguments: ['@entity_type.manager', '@logger.channel.apigee_edge'] diff --git a/modules/apigee_edge_teams/apigee_edge_teams.services.yml b/modules/apigee_edge_teams/apigee_edge_teams.services.yml index bd915b2f..0db1d3f9 100644 --- a/modules/apigee_edge_teams/apigee_edge_teams.services.yml +++ b/modules/apigee_edge_teams/apigee_edge_teams.services.yml @@ -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_delete_action_performer: + class: Drupal\apigee_edge_teams\User\RemoveTeamRolesOfUserSynchronousPostUserDeleteActionPerformer + decorates: apigee_edge.post_user_delete_action_performer + arguments: [ '@apigee_edge_teams.post_user_delete_action_performer.inner', '@entity_type.manager', '@logger.channel.apigee_edge_teams' ] diff --git a/modules/apigee_edge_teams/src/User/RemoveTeamRolesWithUserSynchronousUserRemovalHandler.php b/modules/apigee_edge_teams/src/User/RemoveTeamRolesOfUserSynchronousPostUserDeleteActionPerformer.php similarity index 73% rename from modules/apigee_edge_teams/src/User/RemoveTeamRolesWithUserSynchronousUserRemovalHandler.php rename to modules/apigee_edge_teams/src/User/RemoveTeamRolesOfUserSynchronousPostUserDeleteActionPerformer.php index b8071dba..286267c7 100644 --- a/modules/apigee_edge_teams/src/User/RemoveTeamRolesWithUserSynchronousUserRemovalHandler.php +++ b/modules/apigee_edge_teams/src/User/RemoveTeamRolesOfUserSynchronousPostUserDeleteActionPerformer.php @@ -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; @@ -30,31 +30,31 @@ /** * Ensures team roles of the removed user also get deleted. */ -final class RemoveTeamRolesWithUserSynchronousUserRemovalHandler implements UserRemovalHandlerInterface { +final class RemoveTeamRolesOfUserSynchronousPostUserDeleteActionPerformer 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); } diff --git a/src/User/UserRemovalHandlerInterface.php b/src/User/PostUserDeleteActionPerformerInterface.php similarity index 66% rename from src/User/UserRemovalHandlerInterface.php rename to src/User/PostUserDeleteActionPerformerInterface.php index ee99ec49..4a3110ba 100644 --- a/src/User/UserRemovalHandlerInterface.php +++ b/src/User/PostUserDeleteActionPerformerInterface.php @@ -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; } diff --git a/src/User/RemoveDeveloperWithUserSynchronousUserRemovalHandler.php b/src/User/RemoveRelatedDeveloperAccountSynchronousPostUserDeleteActionPerformer.php similarity index 87% rename from src/User/RemoveDeveloperWithUserSynchronousUserRemovalHandler.php rename to src/User/RemoveRelatedDeveloperAccountSynchronousPostUserDeleteActionPerformer.php index 7daaf6c0..e449affb 100644 --- a/src/User/RemoveDeveloperWithUserSynchronousUserRemovalHandler.php +++ b/src/User/RemoveRelatedDeveloperAccountSynchronousPostUserDeleteActionPerformer.php @@ -35,7 +35,7 @@ * * @see \apigee_edge_user_delete() */ -final class RemoveDeveloperWithUserSynchronousUserRemovalHandler implements UserRemovalHandlerInterface { +final class RemoveRelatedDeveloperAccountSynchronousPostUserDeleteActionPerformer implements PostUserDeleteActionPerformerInterface { /** * Entity type manager. @@ -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. @@ -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).
@backtrace_string
', $context); }