You are here

public function GoogleApiClient::save in Google API PHP Client 8.3

Same name and namespace in other branches
  1. 8.4 src/Entity/GoogleApiClient.php \Drupal\google_api_client\Entity\GoogleApiClient::save()
  2. 8.2 src/Entity/GoogleApiClient.php \Drupal\google_api_client\Entity\GoogleApiClient::save()

Return value

int Id for the GoogleApiClient being saved.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

Overrides EntityBase::save

File

src/Entity/GoogleApiClient.php, line 579

Class

GoogleApiClient
Defines the GoogleApiClient entity.

Namespace

Drupal\google_api_client\Entity

Code

public function save() {

  // Skip for new entity.
  if ($this
    ->isNew()) {
    if ($this
      ->getAccessToken() == '' || $this
      ->getAccessToken() == NULL) {
      $this
        ->setAccessToken('');
      $this
        ->setAuthenticated(FALSE);
    }
    return parent::save();
  }
  $original = $this->original ? $this->original : NULL;
  if (!$original) {

    // Usually this should exist in save but still keeping it safe.
    $id = $this
      ->getOriginalId() !== NULL ? $this
      ->getOriginalId() : $this
      ->id();
    $original = $this
      ->entityTypeManager()
      ->getStorage($this
      ->getEntityTypeId())
      ->loadUnchanged($id);
  }
  if ($original && ($original
    ->getServices() != $this
    ->getServices() || $original
    ->getClientId() != $this
    ->getClientId() || $original
    ->getClientSecret() != $this
    ->getClientSecret() || $original
    ->getScopes() != $this
    ->getScopes() || $original
    ->getDeveloperKey() != $this
    ->getDeveloperKey())) {

    // If the google_api_client isi modified it needs to be re-authenticated.
    $this
      ->setAccessToken('');
    $this
      ->setAuthenticated(FALSE);
  }
  return parent::save();
}