<commit_before><?php

namespace Evolution7\SocialApi\Service;

use Evolution7\SocialApi\Config\ConfigInterface;
use Evolution7\SocialApi\Token\RequestToken;
use Evolution7\SocialApi\Token\AccessToken;
use OAuth\Common\Consumer\Credentials as OAuthCredentials;
use OAuth\Common\Storage\Memory as OAuthStorageMemory;
use OAuth\Common\Service\ServiceFactory as OAuthServiceFactory;

class Service implements ServiceInterface
{

  protected $config;

  public function __construct(ConfigInterface $config)
  {
    $this->config = $config;
  }

  public function getConfig()
  {
    return $this->config;
  }

  public function getService()
  {

    // Get config
    $config = $this->getConfig();

    // Build new OAuth library service instance
    $credentials = new OAuthCredentials(
      $config->getApiKey(),
      $config->getApiSecret(),
      $config->getReturnUrl()
    );
    $storage = new OAuthStorageMemory();
    $oauthServiceFactory = new OAuthServiceFactory();
    $oauthService = $oauthServiceFactory->createService(
      $config->getPlatform(),
      $credentials,
      $storage,
      $config->getApiScopes()
    );
    return $oauthService;

  }

  public function getAuthRequest()
  {

    // Get OAuth libray Service instance
    $oauthService = $this->getService();

    // Check if OAuth1
    if (method_exists($oauthService, 'requestRequestToken')) {

      // Generate request token via service provider API
      $token = $oauthService->requestRequestToken(); // OAuth V1 Only
      $requestToken = $token->getRequestToken();
      $requestSecret = $token->getRequestTokenSecret();

      // Generate url
      $url = $oauthService->getAuthorizationUri(
        array('oauth_token' => $requestToken) // OAuth V1 Only
        );  

    } else {
      
      // Generate url
      $url = $oauthService->getAuthorizationUri();
      $requestToken = null;
      $requestSecret = null;

    }

    // Build RequestToken and return
    return new RequestToken($requestToken, $requestSecret, $url->__toString());

  }

  public function getAuthAccess(RequestToken $requestToken, $oauthToken, $oauthVerifier, $code)
  {

    // Get OAuth libray Service instance
    $oauthService = $this->getService();

    // Determine OAuth version
    if (!empty($oauthVerifier) && empty($code)) {
      $version = 1;
    } else if (!empty($oauthVerifier) && empty($code)) {
      $version = 2;
    } else {
      throw new \Exception('$oauthToken and $code are mutually exclusive.');
    }

    // Check for token mismatch<commit_msg>Fixed check in getAuthAccess<commit_after><?php

namespace Evolution7\SocialApi\Service;

use Evolution7\SocialApi\Config\ConfigInterface;
use Evolution7\SocialApi\Token\RequestToken;
use Evolution7\SocialApi\Token\AccessToken;
use OAuth\Common\Consumer\Credentials as OAuthCredentials;
use OAuth\Common\Storage\Memory as OAuthStorageMemory;
use OAuth\ServiceFactory as OAuthServiceFactory;

class Service implements ServiceInterface
{

  protected $config;

  public function __construct(ConfigInterface $config)
  {
    $this->config = $config;
  }

  public function getConfig()
  {
    return $this->config;
  }

  public function getService()
  {

    // Get config
    $config = $this->getConfig();

    // Build new OAuth library service instance
    $credentials = new OAuthCredentials(
      $config->getApiKey(),
      $config->getApiSecret(),
      $config->getReturnUrl()
    );
    $storage = new OAuthStorageMemory();
    $oauthServiceFactory = new OAuthServiceFactory();
    $oauthService = $oauthServiceFactory->createService(
      $config->getPlatform(),
      $credentials,
      $storage,
      $config->getApiScopes()
    );
    return $oauthService;

  }

  public function getAuthRequest()
  {

    // Get OAuth libray Service instance
    $oauthService = $this->getService();

    // Check if OAuth1
    if (method_exists($oauthService, 'requestRequestToken')) {

      // Generate request token via service provider API
      $token = $oauthService->requestRequestToken(); // OAuth V1 Only
      $requestToken = $token->getRequestToken();
      $requestSecret = $token->getRequestTokenSecret();

      // Generate url
      $url = $oauthService->getAuthorizationUri(
        array('oauth_token' => $requestToken) // OAuth V1 Only
        );  

    } else {
      
      // Generate url
      $url = $oauthService->getAuthorizationUri();
      $requestToken = null;
      $requestSecret = null;

    }

    // Build RequestToken and return
    return new RequestToken($requestToken, $requestSecret, $url->__toString());

  }

  public function getAuthAccess(RequestToken $requestToken, $oauthToken, $oauthVerifier, $code)
  {

    // Get OAuth libray Service instance
    $oauthService = $this->getService();

    // Determine OAuth version
    if (!empty($oauthVerifier) && empty($code)) {
      $version = 1;
    } else if (empty($oauthVerifier) && !empty($code)) {
      $version = 2;
    } else {
      throw new \Exception('$oauthToken and $code are mutually exclusive.');
    }

    // Check for token mismatch
{'fraction_non_alphanumeric': 0.1173228346456693, 'fraction_numerical': 0.003543307086614173, 'mean_word_length': 3.2200996677740865, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5923315', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1442, 'n_words': 344}