You are here

protected function TeamStorage::doPostSave in Apigee Edge 8

Performs post save entity processing.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The saved entity.

bool $update: Specifies whether the entity is being updated or created.

Overrides EntityStorageBase::doPostSave

File

modules/apigee_edge_teams/src/Entity/Storage/TeamStorage.php, line 147

Class

TeamStorage
Entity storage implementation for teams.

Namespace

Drupal\apigee_edge_teams\Entity\Storage

Code

protected function doPostSave(EntityInterface $entity, $update) {

  /** @var \Drupal\apigee_edge_teams\Entity\TeamInterface $entity */
  if (!$update) {

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

    /** @var \Drupal\apigee_edge_teams\Entity\TeamMemberRoleInterface[] $team_roles_by_teams */
    $team_roles_by_teams = $team_member_role_storage
      ->loadByTeam($entity);
    if ($team_roles_by_teams) {

      // Teams (Companies) can be deleted outside of Drupal so it could
      // happen that remnant team member roles exist in database when
      // a new team gets created with a previously used team id.
      $context = [
        '%team' => "{$entity->label()} ({$entity->id()})",
        'link' => $entity
          ->toLink($this
          ->t('Members'), 'members')
          ->toString(),
      ];
      $this->logger
        ->warning('Integrity check: Remnant team member roles found for new %team team.', $context);
      $success = TRUE;
      foreach ($team_roles_by_teams as $team_member_role) {
        try {
          $team_member_role
            ->delete();
        } catch (\Exception $exception) {
          $success = FALSE;
          $context += Error::decodeException($exception);
          $this->logger
            ->warning('Failed to remove remnant developer role from new %team team. @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
        }
      }
      if ($success) {
        $this->logger
          ->info('Integrity check: Successfully removed all remnant team member roles in association with the new %team team.', $context);
      }
      else {
        $this->logger
          ->critical('Integrity check: Failed to remove all remnant team member roles from the database for the new %team team.', $context);
      }
    }
  }
  parent::doPostSave($entity, $update);
}