public function TeamMemberRoleStorage::addTeamRoles in Apigee Edge 8
Adds team roles to a developer in a team.
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.
Throws
\Drupal\apigee_edge_teams\Exception\InvalidArgumentException
\Drupal\Core\Entity\EntityStorageException
Overrides TeamMemberRoleStorageInterface::addTeamRoles
File
- modules/
apigee_edge_teams/ src/ Entity/ Storage/ TeamMemberRoleStorage.php, line 146
Class
- TeamMemberRoleStorage
- Entity storage class for team member role entities.
Namespace
Drupal\apigee_edge_teams\Entity\StorageCode
public function addTeamRoles(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.");
}
// Indicates whether a new team member role entity had to be created
// or not.
/** @var \Drupal\apigee_edge_teams\Entity\TeamMemberRoleInterface $team_member_roles */
$team_member_roles = $this
->loadByDeveloperAndTeam($account, $team);
if ($team_member_roles === NULL) {
$team_member_roles = $this
->create([
'uid' => [
'target_id' => $account
->id(),
],
'team' => [
'target_id' => $team
->id(),
],
]);
}
// Make sure we only store unique values in the field.
$existing_roles = array_map(function ($item) {
return $item['target_id'];
}, $team_member_roles->roles
->getValue());
$unique_roles = array_diff(array_unique($roles), $existing_roles);
foreach ($unique_roles as $role) {
$team_member_roles->roles[] = [
'target_id' => $role,
];
}
try {
$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 saved. Roles: %roles. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
throw $exception;
}
return $team_member_roles;
}