You are here

class TwitterAuthManager in Open Social 8.9

Same name and namespace in other branches
  1. 8 modules/custom/social_auth_twitter/src/TwitterAuthManager.php \Drupal\social_auth_twitter\TwitterAuthManager
  2. 8.2 modules/custom/social_auth_twitter/src/TwitterAuthManager.php \Drupal\social_auth_twitter\TwitterAuthManager
  3. 8.3 modules/custom/social_auth_twitter/src/TwitterAuthManager.php \Drupal\social_auth_twitter\TwitterAuthManager
  4. 8.4 modules/custom/social_auth_twitter/src/TwitterAuthManager.php \Drupal\social_auth_twitter\TwitterAuthManager
  5. 8.5 modules/custom/social_auth_twitter/src/TwitterAuthManager.php \Drupal\social_auth_twitter\TwitterAuthManager
  6. 8.6 modules/custom/social_auth_twitter/src/TwitterAuthManager.php \Drupal\social_auth_twitter\TwitterAuthManager
  7. 8.7 modules/custom/social_auth_twitter/src/TwitterAuthManager.php \Drupal\social_auth_twitter\TwitterAuthManager
  8. 8.8 modules/custom/social_auth_twitter/src/TwitterAuthManager.php \Drupal\social_auth_twitter\TwitterAuthManager

Manages the authorization process before getting a long lived access token.

Hierarchy

Expanded class hierarchy of TwitterAuthManager

2 files declare their use of TwitterAuthManager
TwitterAuthController.php in modules/custom/social_auth_twitter/src/Controller/TwitterAuthController.php
TwitterLinkController.php in modules/custom/social_auth_twitter/src/Controller/TwitterLinkController.php
1 string reference to 'TwitterAuthManager'
social_auth_twitter.services.yml in modules/custom/social_auth_twitter/social_auth_twitter.services.yml
modules/custom/social_auth_twitter/social_auth_twitter.services.yml
1 service uses TwitterAuthManager
social_auth_twitter.auth_manager in modules/custom/social_auth_twitter/social_auth_twitter.services.yml
\Drupal\social_auth_twitter\TwitterAuthManager

File

modules/custom/social_auth_twitter/src/TwitterAuthManager.php, line 14

Namespace

Drupal\social_auth_twitter
View source
class TwitterAuthManager extends AuthManager {

  /**
   * {@inheritdoc}
   */
  public function getSocialNetworkKey() {
    return TwitterAuthSettings::getSocialNetworkKey();
  }

  /**
   * {@inheritdoc}
   */
  public function setSdk($sdk) {
    if (!$sdk instanceof TwitterOAuth) {
      throw new InvalidArgumentException('SDK object should be instance of \\Abraham\\TwitterOAuth\\TwitterOAuth class');
    }
    $this->sdk = $sdk;
  }

  /**
   * {@inheritdoc}
   */
  public function getAuthenticationUrl($type, array $scope = [
    'read',
  ]) {
    $request_token = $this->sdk
      ->oauth('oauth/request_token', [
      'oauth_callback' => $this
        ->getRedirectUrl($type),
      'x_auth_access_type' => current($scope),
    ]);
    $data_handler = \Drupal::service('plugin.network.manager')
      ->createInstance('social_auth_twitter')
      ->getDataHandler();
    $data_handler
      ->set('oauth_token', $request_token['oauth_token']);
    $data_handler
      ->set('oauth_token_secret', $request_token['oauth_token_secret']);
    return $this->sdk
      ->url('oauth/authorize', [
      'oauth_token' => $request_token['oauth_token'],
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getAccessToken($type) {

    // This method should not used for Twitter.
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getProfile() {
    try {
      $this->profile = $this->sdk
        ->get('account/verify_credentials', [
        'include_email' => 'true',
        'include_entities' => 'false',
        'skip_status' => 'true',
      ]);
      return $this->profile;
    } catch (TwitterOAuthException $e) {
      $this->loggerFactory
        ->get('social_auth_twitter')
        ->error('Could not load Twitter user profile: TwitterOAuthException: @message', [
        '@message' => $e
          ->getMessage(),
      ]);
      return NULL;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getProfilePicture() {
    if (($profile = $this
      ->getProfile()) && isset($profile->screen_name) && empty($profile->default_profile_image)) {
      return "https://twitter.com/{$profile->screen_name}/profile_image?size=original";
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setAccessToken($access_token) {
    $this->sdk
      ->setOauthToken($access_token['oauth_token'], $access_token['oauth_token_secret']);
  }

  /**
   * {@inheritdoc}
   */
  public function getAccountId() {
    if (($profile = $this
      ->getProfile()) && isset($profile->id)) {
      return $profile->id;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getFirstName() {

    // Twitter doesn't contain first name.
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getLastName() {

    // Twitter doesn't contain last name.
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getUsername() {
    if (($profile = $this
      ->getProfile()) && isset($profile->screen_name)) {
      return $profile->screen_name;
    }
    return parent::getUsername();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuthManager::$entityFieldManager protected property
AuthManager::$fieldPicture protected property Contains the field definition with a profile picture.
AuthManager::$loggerFactory protected property
AuthManager::$profile protected property Contains object of a profile received from a social network.
AuthManager::$sdk protected property Object of SDK to work with API of social network.
AuthManager::$urlGenerator protected property
AuthManager::getPreferredResolution public function Determines preferred profile pic resolution from account settings. Overrides AuthManagerInterface::getPreferredResolution
AuthManager::getRedirectUrl public function Returns URL to authorize/registration depending on type. Overrides AuthManagerInterface::getRedirectUrl
AuthManager::setFieldPicture public function Set an instance of a field definition that contains picture. Overrides AuthManagerInterface::setFieldPicture
AuthManager::__construct public function AuthManager constructor.
TwitterAuthManager::getAccessToken public function Reads user's access token from social network. Overrides AuthManagerInterface::getAccessToken
TwitterAuthManager::getAccountId public function Returns an account ID on a social network. Overrides AuthManagerInterface::getAccountId
TwitterAuthManager::getAuthenticationUrl public function Returns the login URL where user will be redirected for authentication. Overrides AuthManagerInterface::getAuthenticationUrl
TwitterAuthManager::getFirstName public function Returns first name on a social network if it possible. Overrides AuthManagerInterface::getFirstName
TwitterAuthManager::getLastName public function Returns last name on a social network if it possible. Overrides AuthManagerInterface::getLastName
TwitterAuthManager::getProfile public function Returns object of a user profile. Overrides AuthManagerInterface::getProfile
TwitterAuthManager::getProfilePicture public function Returns URL of a profile picture. Overrides AuthManagerInterface::getProfilePicture
TwitterAuthManager::getSocialNetworkKey public function Returns key-name of a social network. Overrides AuthManagerInterface::getSocialNetworkKey
TwitterAuthManager::getUsername public function Returns user on a social network if it possible. Overrides AuthManager::getUsername
TwitterAuthManager::setAccessToken public function Set access token to AuthManager to use it for API calls. Overrides AuthManagerInterface::setAccessToken
TwitterAuthManager::setSdk public function Set instance of SDK. Overrides AuthManagerInterface::setSdk