You are here

public function TeamMembershipManager::removeMembers in Apigee Edge 8

Same name in this branch
  1. 8 modules/apigee_edge_teams/src/TeamMembershipManager.php \Drupal\apigee_edge_teams\TeamMembershipManager::removeMembers()
  2. 8 modules/apigee_edge_actions/src/TeamMembershipManager.php \Drupal\apigee_edge_actions\TeamMembershipManager::removeMembers()

Removes members from a team.

Parameters

string $team: Name of a team.

array $developers: Array of developer email addresses.

Overrides TeamMembershipManagerInterface::removeMembers

File

modules/apigee_edge_teams/src/TeamMembershipManager.php, line 132

Class

TeamMembershipManager
Service that makes easier to work with company (team) memberships.

Namespace

Drupal\apigee_edge_teams

Code

public function removeMembers(string $team, array $developers) : void {
  $controller = $this->companyMembersControllerFactory
    ->companyMembersController($team);

  /** @var \Drupal\apigee_edge_teams\Entity\Storage\TeamMemberRoleStorageInterface $team_member_role_storage */
  $team_member_role_storage = $this->entityTypeManager
    ->getStorage('team_member_role');

  /** @var \Drupal\user\UserInterface[] $users_by_mail */
  $users_by_mail = array_reduce($this->entityTypeManager
    ->getStorage('user')
    ->loadByProperties([
    'mail' => $developers,
  ]), function (array $carry, UserInterface $user) {
    $carry[$user
      ->getEmail()] = $user;
    return $carry;
  }, []);
  foreach ($developers as $developer) {
    $controller
      ->removeMember($developer);

    // Remove team member's roles from Drupal.
    if (array_key_exists($developer, $users_by_mail)) {

      /** @var \Drupal\apigee_edge_teams\Entity\TeamMemberRoleInterface[] $team_member_roles_in_teams */
      $team_member_roles_in_teams = $team_member_role_storage
        ->loadByDeveloper($users_by_mail[$developer]);
      foreach ($team_member_roles_in_teams as $team_member_roles_in_team) {
        try {
          $team_member_roles_in_team
            ->delete();
        } catch (EntityStorageException $e) {
          $this->logger
            ->critical("Failed to remove %developer team member's roles in %team team with its membership.", [
            '%developer' => $developer,
            '%team' => $team_member_roles_in_team
              ->getTeam()
              ->id(),
          ]);
        }
      }
    }
  }
  $this
    ->invalidateCaches($team, $developers);
}