You are here

public function RevocableTokenRepositoryTrait::persistNew in Simple OAuth (OAuth2) & OpenID Connect 8.3

Same name and namespace in other branches
  1. 8.4 src/Repositories/RevocableTokenRepositoryTrait.php \Drupal\simple_oauth\Repositories\RevocableTokenRepositoryTrait::persistNew()
  2. 8.2 src/Repositories/RevocableTokenRepositoryTrait.php \Drupal\simple_oauth\Repositories\RevocableTokenRepositoryTrait::persistNew()
  3. 5.x src/Repositories/RevocableTokenRepositoryTrait.php \Drupal\simple_oauth\Repositories\RevocableTokenRepositoryTrait::persistNew()
3 calls to RevocableTokenRepositoryTrait::persistNew()
AccessTokenRepository::persistNewAccessToken in src/Repositories/AccessTokenRepository.php
AuthCodeRepository::persistNewAuthCode in simple_oauth_extras/src/Repositories/AuthCodeRepository.php
RefreshTokenRepository::persistNewRefreshToken in src/Repositories/RefreshTokenRepository.php

File

src/Repositories/RevocableTokenRepositoryTrait.php, line 51

Class

RevocableTokenRepositoryTrait
Common methods for token repositories on different grants.

Namespace

Drupal\simple_oauth\Repositories

Code

public function persistNew($token_entity) {
  if (!is_a($token_entity, static::$entity_interface)) {
    throw new \InvalidArgumentException(sprintf('%s does not implement %s.', get_class($token_entity), static::$entity_interface));
  }
  $values = $this->serializer
    ->normalize($token_entity);
  $values['bundle'] = static::$bundle_id;
  $new_token = $this->entityTypeManager
    ->getStorage(static::$entity_type_id)
    ->create($values);
  if ($token_entity instanceof RefreshTokenEntityInterface) {
    $access_token = $token_entity
      ->getAccessToken();
    if (!empty($access_token
      ->getUserIdentifier())) {
      $new_token
        ->set('auth_user_id', $access_token
        ->getUserIdentifier());
    }
  }
  $new_token
    ->save();
}