<commit_before><?php

namespace Borfast\Socializr\Connectors;

use Borfast\Socializr\Post;
use Borfast\Socializr\Profile;
use Borfast\Socializr\Response;
use Borfast\Socializr\Connectors\AbstractConnector;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Token\Exception\ExpiredTokenException;

class Twitter extends AbstractConnector
{
    public static $provider = 'Twitter';

    protected $user_id;
    protected $screen_name;


    public function request($path, $method = 'GET', $params = [], $headers = [])
    {
        $result = parent::request($path, $method, $params, $headers);
        $json_result = json_decode($result, true);

        // Since Twitter can return more than one error, we'll need to check if
        // we have an expired token separately.
        $error_type = 'generic';
        if (isset($json_result['errors'])) {
            $errors = $json_result['errors'];
            $msg = 'Error accessing Twitter. Error count: %s.';
            $msg = sprintf($msg, count($errors));
            $i = 0;

            foreach ($errors as $error) {
                $msg2 = "\nError %d -- Error code: %s. Message: %s";
                $msg .= sprintf($msg2, $i, $error['code'], $error['message']);
                $i++;

                // If it's an expired token...
                if ($error['code'] == 89) {
                    $error_type = 'expired';
                }
            }

            if ($error_type == 'expired') {
                throw new ExpiredTokenException($msg);
            } else {
                throw new \Exception($msg);
            }
        }

        return $result;
    }


    public function post(Post $post)
    {
        $path = '/statuses/update.json';
        $method = 'POST';
        $params = array(
            'status' => $post->body,
        );

        $result = $this->request($path, $method, $params);

        $response = new Response;
        $response->setRawResponse(json_encode($result));
        $result_json = json_decode($result);
        $response->setProvider('Twitter');
        $response->setPostId($result_json->id_str);

        return $response;
    }


    /**
     * Twitter needs an extra step for authentication before providing an
     * authorization URL.
     *
     * @author <NAME>
     */
    public function getAuthorizationUri(array $params = array())
    {
        $token = $this->service->requestRequestToken();
        $extra = array('oauth_token' => $token->getRequestToken());
        return parent::getAuthorizationUri($extra);
    }


    /**
     * Retrieve the auth token from the provider's response and store it.
     */
    public function storeOauthToken($params)
    {
        $token = $this->service->getStorage()->retrieveAccessToken('Twitter');
        $result = $this->service->requestAccessToken($params['oauth_token'], $params['oauth_verifier'], $token->getRequestTokenSecret());

        $extra_params = $result->getExtraParams();
        $this->user_id = $extra_params['user_id'];
        $this->screen_name = $extra_params['screen_name'];
    }

    public function getProfile()
    {
        $path = '/account/verify_credentials.json?skip_status=1';
        $result = $this->request($path);
        $profile_json = json_decode($result, true);

        $mapping = [
            'id' => 'id_str',
            // 'email' => 'email',
            'name' => 'name',
            'first_name' => 'first_name',
            'middle_name' => 'middle_name',
            'last_name' => 'last_name',
            'username' => 'screen_name',
            'link' => 'link'
        ];

        $profile = Profile::create($mapping, $profile_json);
        $profile->provider = static::$provider;
        $profile->raw_response = $result;

        return $profile;
    }

    public function getStats()
    {
        $path = '/followers/ids.json?user_id='.$this->id;
        $response = $this->request($path);
        $response = json_decode($response);
        $response = count($response->ids);
        return $response;
    }


    /***************************************************************************
     *
     * From here on these are Twitter-specific methods that should not be
     * accessed from other classes.
     *
     **************************************************************************/

    public function getTweets()
    {
        $path = '/statuses/user_timeline.json?user_id='.$this->id;
        $response = $this->request($path);
        $response = json_decode($response);
    }
}<commit_msg>Remove unused code from Twitter Connector<commit_after><?php

namespace Borfast\Socializr\Connectors;

use Borfast\Socializr\Post;
use Borfast\Socializr\Profile;
use Borfast\Socializr\Response;
use Borfast\Socializr\Connectors\AbstractConnector;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Token\Exception\ExpiredTokenException;

class Twitter extends AbstractConnector
{
    public static $provider = 'Twitter';

    protected $user_id;
    protected $screen_name;


    public function request($path, $method = 'GET', $params = [], $headers = [])
    {
        $result = parent::request($path, $method, $params, $headers);
        $json_result = json_decode($result, true);

        // Since Twitter can return more than one error, we'll need to check if
        // we have an expired token separately.
        $error_type = 'generic';
        if (isset($json_result['errors'])) {
            $errors = $json_result['errors'];
            $msg = 'Error accessing Twitter. Error count: %s.';
            $msg = sprintf($msg, count($errors));
            $i = 0;

            foreach ($errors as $error) {
                $msg2 = "\nError %d -- Error code: %s. Message: %s";
                $msg .= sprintf($msg2, $i, $error['code'], $error['message']);
                $i++;

                // If it's an expired token...
                if ($error['code'] == 89) {
                    $error_type = 'expired';
                }
            }

            if ($error_type == 'expired') {
                throw new ExpiredTokenException($msg);
            } else {
                throw new \Exception($msg);
            }
        }

        return $result;
    }


    public function post(Post $post)
    {
        $path = '/statuses/update.json';
        $method = 'POST';
        $params = array(
            'status' => $post->body,
        );

        $result = $this->request($path, $method, $params);

        $response = new Response;
        $response->setRawResponse(json_encode($result));
        $result_json = json_decode($result);
        $response->setProvider('Twitter');
        $response->setPostId($result_json->id_str);

        return $response;
    }


    /**
     * Twitter needs an extra step for authentication before providing an
     * authorization URL.
     *
     * @author <NAME>
     */
    public function getAuthorizationUri(array $params = array())
    {
        $token = $this->service->requestRequestToken();
        $extra = array('oauth_token' => $token->getRequestToken());
        return parent::getAuthorizationUri($extra);
    }


    /**
     * Retrieve the auth token from the provider's response and store it.
     */
    public function storeOauthToken($params)
    {
        $token = $this->service->getStorage()->retrieveAccessToken('Twitter');
        $result = $this->service->requestAccessToken($params['oauth_token'], $params['oauth_verifier'], $token->getRequestTokenSecret());

        $extra_params = $result->getExtraParams();
        $this->user_id = $extra_params['user_id'];
        $this->screen_name = $extra_params['screen_name'];
    }

    public function getProfile()
    {
        $path = '/account/verify_credentials.json?skip_status=1';
        $result = $this->request($path);
        $profile_json = json_decode($result, true);

        $mapping = [
            'id' => 'id_str',
            // 'email' => 'email',
            'name' => 'name',
            'first_name' => 'first_name',
            'middle_name' => 'middle_name',
            'last_name' => 'last_name',
            'username' => 'screen_name',
            'link' => 'link'
        ];

        $profile = Profile::create($mapping, $profile_json);
        $profile->provider = static::$provider;
        $profile->raw_response = $result;

        return $profile;
    }

    public function getStats()
    {
        $path = '/followers/ids.json?user_id='.$this->id;
        $response = $this->request($path);
        $response = json_decode($response);
        $response = count($response->ids);
        return $response;
    }
}
{'fraction_non_alphanumeric': 0.1611297603889339, 'fraction_numerical': 0.0013890496585252923, 'mean_word_length': 2.2690124858115777, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 75, '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': '1176858', 'n_tokens_mistral': 2468, 'n_tokens_neox': 2374, 'n_words': 521}