You are here

public function TeamMemberRoleStorage::removeTeamRoles in Apigee Edge 8

Removes team roles of a developer within a team.

If you would like to remove a developer from a team (remove its "member" team role) use the team membership manager service.

Parameters

\Drupal\Core\Session\AccountInterface $account: User entity object of a developer.

\Drupal\apigee_edge_teams\Entity\TeamInterface $team: Team entity object.

string[] $roles: Array of team role entity ids.

Return value

\Drupal\apigee_edge_teams\Entity\TeamMemberRoleInterface The updated team member role entity.

Overrides TeamMemberRoleStorageInterface::removeTeamRoles

File

modules/apigee_edge_teams/src/Entity/Storage/TeamMemberRoleStorage.php, line 197

Class

TeamMemberRoleStorage
Entity storage class for team member role entities.

Namespace

Drupal\apigee_edge_teams\Entity\Storage

Code

public function removeTeamRoles(AccountInterface $account, TeamInterface $team, array $roles) : TeamMemberRoleInterface {
  if ($account
    ->isAnonymous()) {
    throw new InvalidArgumentException('Anonymous user can not be member of a team.');
  }
  try {
    $developer_team_ids = $this->teamMembershipManager
      ->getTeams($account
      ->getEmail());
  } catch (\Exception $e) {
    $developer_team_ids = [];
  }
  if (!in_array($team
    ->id(), $developer_team_ids)) {
    throw new InvalidArgumentException("{$account->getEmail()} is not member of {$team->id()} team.");
  }

  /** @var \Drupal\apigee_edge_teams\Entity\TeamMemberRoleInterface $team_member_roles */
  $team_member_roles = $this
    ->loadByDeveloperAndTeam($account, $team);
  if ($team_member_roles === NULL) {
    throw new InvalidArgumentException("{$account->getEmail()} does not have team roles in {$team->id()} team.");
  }
  $team_member_roles->roles = array_filter($team_member_roles->roles
    ->getValue(), function (array $item) use ($roles) {
    return !in_array($item['target_id'], $roles);
  });
  try {

    // If the developer does not have any roles in the team anymore then
    // remove its team member role entity.
    if (empty($team_member_roles->roles
      ->getValue())) {
      $team_member_roles
        ->delete();
    }
    else {
      $team_member_roles
        ->save();
    }
  } catch (EntityStorageException $exception) {
    $context = [
      '%developer' => $account
        ->getEmail(),
      '%team_id' => $team
        ->id(),
      '%roles' => implode(',', $roles),
      'link' => $team
        ->toLink($this
        ->t('Members'), 'members')
        ->toString(),
    ];
    $context += Error::decodeException($exception);
    $this->logger
      ->warning('%developer team member roles in %team_id team could not be removed. Roles: %roles. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
    throw $exception;
  }
  return $team_member_roles;
}