<commit_before><?php

namespace AppBundle\Service;

use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;

class TwitterAPI
{
    /**
     * @var HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken
     */
    protected $oauthToken;

    protected $consumer_key = ''; // api key
    protected $consumer_secret = ''; // api secret
    protected $bearer_token = '';

    protected $request_url = ''; // decide by api call method

    /**
     * @param TokenStorage $token_storage $this->container->get('security.token_storage')
     * @param array $key_and_token twitter api key and tokens
     */
    public function __construct(TokenStorage $token_storage, array $key_and_token)
    {
        if (($oauthToken = $token_storage->getToken()) instanceof OAuthToken === false) {
            throw new InvalidArgumentException(sprintf('Object get from tokenstrage was not a OAuthToken. getting "%s" object.', get_class($oauthToken)));
        }

        $this->oauthToken = $oauthToken;
        $this->consumer_key = $key_and_token['consumer_key'];
        $this->consumer_secret = $key_and_token['consumer_secret'];
        $this->bearer_token = $key_and_token['bearer_token'];
    }

    /**
     * @return TBD
     */
    public function getTodaysTweet()
    {
        $database['todays_since_id'] = '';

        // 今日の始点つぶやきのsince_idが無ければタイムラインをまるまる取得してsince_idを計算する
        if (!$database['todays_since_id']) {
            $get_query = array(
              'user_id' => $this->oauthToken->getRawToken()['user_id'],
              'count' => '200',
            );
            $decoded_json = $this->callUserTimeline($get_query);
            //今日一番最初のつぶやきのsince_id抽出処理

            //抽出したsince_idをDBに登録

            //今日のつぶやき一覧をreturn

        // since_idがあればget_queryに指定して今日のつぶやき一覧をapiから取得
        } else {
            $get_query = array(
              'user_id' => $this->oauthToken->getRawToken()['user_id'],
              'count' => '200',
            );

            return $decoded_json = $this->callUserTimeline($get_query);
        }
    }

    /**
    * call api https://api.twitter.com/1.1/statuses/user_timeline.json
    *
    * @param array $get_query
    * @return stdClass $decoded_json
    */
    protected function callUserTimeline(array $get_query = array())
    {
        $this->request_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';

        if ($get_query) {
            $this->request_url = $this->concatGetQuery($this->request_url, $get_query);
        }

        $context = $this->createBearerAuthContext();

        $response_json = @file_get_contents($this->request_url, false, stream_context_create($context));
        $decoded_json = json_decode($response_json);

        return $decoded_json;
    }

    /**
     * @param string date format 'Y-m-d'
     * @return json tweet
     */
    protected function getTweetByDate($date)
    {
        $this->request_url = 'https://api.twitter.com/1.1/search/tweets.json';
        $get_query = array(<commit_msg>[TwitterAPI.php] Aligned api call methods name to api url schema<commit_after><?php

namespace AppBundle\Service;

use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;

class TwitterAPI
{
    /**
     * @var HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken
     */
    protected $oauthToken;

    protected $consumer_key = ''; // api key
    protected $consumer_secret = ''; // api secret
    protected $bearer_token = '';

    protected $request_url = ''; // decide by api call method

    /**
     * @param TokenStorage $token_storage $this->container->get('security.token_storage')
     * @param array $key_and_token twitter api key and tokens
     */
    public function __construct(TokenStorage $token_storage, array $key_and_token)
    {
        if (($oauthToken = $token_storage->getToken()) instanceof OAuthToken === false) {
            throw new InvalidArgumentException(sprintf('Object get from tokenstrage was not a OAuthToken. getting "%s" object.', get_class($oauthToken)));
        }

        $this->oauthToken = $oauthToken;
        $this->consumer_key = $key_and_token['consumer_key'];
        $this->consumer_secret = $key_and_token['consumer_secret'];
        $this->bearer_token = $key_and_token['bearer_token'];
    }

    /**
     * @return TBD
     */
    public function getTodaysTweet()
    {
        $database['todays_since_id'] = '';

        // 今日の始点つぶやきのsince_idが無ければタイムラインをまるまる取得してsince_idを計算する
        if (!$database['todays_since_id']) {
            $get_query = array(
              'user_id' => $this->oauthToken->getRawToken()['user_id'],
              'count' => '200',
            );
            $decoded_json = $this->callStatusesUserTimeline($get_query);
            //今日一番最初のつぶやきのsince_id抽出処理

            //抽出したsince_idをDBに登録

            //今日のつぶやき一覧をreturn

        // since_idがあればget_queryに指定して今日のつぶやき一覧をapiから取得
        } else {
            $get_query = array(
              'user_id' => $this->oauthToken->getRawToken()['user_id'],
              'count' => '200',
            );

            return $decoded_json = $this->callStatusesUserTimeline($get_query);
        }
    }

    /**
    * call api https://api.twitter.com/1.1/statuses/user_timeline.json
    *
    * @param array $get_query
    * @return stdClass $decoded_json
    */
    protected function callStatusesUserTimeline(array $get_query = array())
    {
        $this->request_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';

        if ($get_query) {
            $this->request_url = $this->concatGetQuery($this->request_url, $get_query);
        }

        $context = $this->createBearerAuthContext();

        $response_json = @file_get_contents($this->request_url, false, stream_context_create($context));
        $decoded_json = json_decode($response_json);

        return $decoded_json;
    }

    /**
     * @param string date format 'Y-m-d'
     * @return json tweet
     */
    protected function callTweetByDate($date)
    {
        $this->request_url = 'https://api.twitter.com/1.1/search/tweets.json';
        $get_query = array(
{'fraction_non_alphanumeric': 0.12999690114657578, 'fraction_numerical': 0.0037186241090796405, 'mean_word_length': 2.7204610951008648, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 6, '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': '6629424', 'n_tokens_mistral': 2135, 'n_tokens_neox': 2075, 'n_words': 318}