You are here

public function TeamInvitationSubscriber::onAccepted in Apigee Edge 8

Same name in this branch
  1. 8 modules/apigee_edge_teams/src/EventSubscriber/TeamInvitationSubscriber.php \Drupal\apigee_edge_teams\EventSubscriber\TeamInvitationSubscriber::onAccepted()
  2. 8 modules/apigee_edge_teams/tests/modules/apigee_edge_teams_invitation_test/src/EventSubscriber/TeamInvitationSubscriber.php \Drupal\apigee_edge_teams_invitation_test\EventSubscriber\TeamInvitationSubscriber::onAccepted()

Callback for on accepted event.

Parameters

\Drupal\apigee_edge_teams\Event\TeamInvitationEventInterface $event: The event.

File

modules/apigee_edge_teams/src/EventSubscriber/TeamInvitationSubscriber.php, line 126

Class

TeamInvitationSubscriber
Event subscriber for team invitation.

Namespace

Drupal\apigee_edge_teams\EventSubscriber

Code

public function onAccepted(TeamInvitationEventInterface $event) {
  $team_invitation = $event
    ->getTeamInvitation();
  if (!$team_invitation
    ->isAccepted()) {
    return;
  }
  $team = $team_invitation
    ->getTeam();
  $context = [
    '@developer' => $team_invitation
      ->getRecipient(),
    '@team' => mb_strtolower($team
      ->getEntityType()
      ->getSingularLabel()),
    '%team_id' => $team
      ->id(),
  ];
  $success = FALSE;
  try {
    $this->teamMembershipManager
      ->addMembers($team
      ->id(), [
      $team_invitation
        ->getRecipient(),
    ]);
    $success = TRUE;
  } catch (\Exception $exception) {
    $context += Error::decodeException($exception);
    $this->logger
      ->error('Failed to add developer to %team_id team. Developer: @developer. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
  }
  if ($success) {
    $selected_roles = array_map(function (TeamRoleInterface $team_member_role) {
      return $team_member_role
        ->id();
    }, $team_invitation
      ->getTeamRoles());

    /** @var \Drupal\user\UserInterface $user */
    $user = user_load_by_mail($team_invitation
      ->getRecipient());
    if (!$user) {
      $this->logger
        ->error('Developer with email %email not found.', [
        '%email' => $team_invitation
          ->getRecipient(),
      ]);
      return;
    }

    /** @var \Drupal\apigee_edge_teams\Entity\TeamMemberRoleInterface $team_member_roles */
    $team_member_roles = $this->teamMemberRoleStorage
      ->loadByDeveloperAndTeam($user, $team);
    if ($team_member_roles !== NULL) {

      // It could happen the a developer got removed from a team (company)
      // outside of Drupal therefore its team member role entity
      // has not been deleted.
      // @see \Drupal\apigee_edge_teams\TeamMembershipManager::removeMembers()
      try {
        $team_member_roles
          ->delete();
      } catch (\Exception $exception) {
        $context += [
          '%developer' => $user
            ->getEmail(),
          '%roles' => implode(', ', array_map(function (TeamRole $role) {
            return $role
              ->label();
          }, $team_member_roles
            ->getTeamRoles())),
          'link' => $team
            ->toLink($this
            ->t('Members'), 'members')
            ->toString(),
        ];
        $context += Error::decodeException($exception);
        $this->logger
          ->error('Integrity check: %developer developer had a team member role entity with "%roles" team roles for %team_id team when it was added to the team. These roles could not been deleted automatically. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
      }
    }
    try {
      $this->teamMemberRoleStorage
        ->addTeamRoles($user, $team, $selected_roles);
    } catch (\Exception $exception) {
      $this->logger
        ->error('Selected roles could not be saved for %user developer.', [
        '%user' => $user
          ->label(),
      ]);
    }
  }
}