<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;
use Doctrine\Bundle\DoctrineBundle\Registry;

class TwitterAPI
{
    /**
    * @var Doctrine\Bundle\DoctrineBundle\Registry
    */
    protected $doctrine;

    /**
    * @var Symfony\Component\Security\Core\Authentication\Token\Storage
    */
    protected $tokenStorage;

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

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

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

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

    /**
     * get today timeline json
     *
     * @return array|null timeline or null
     */
    public function getTodayTimeline()
    {
        $get_query = ['user_id' => $this->tokenStorage->getToken()->getRawToken()['user_id']];
        $today = (new \DateTime())->format('Y-m-d');
        $since_id_at = $this->tokenStorage->getToken()->getUser()->getSinceIdAt();

        // 今日の始点ツイートのsince_idが無ければタイムラインをまるまる取得してsince_idを計算する
        if ($since_id_at === null || $since_id_at->format('Y-m-d') !== $today) {
            $timeline = $this->searchTodayTimeline($get_query);

            return $timeline;
        }
        // since_idがあればget_queryに指定して今日のつぶやき一覧をapiから取得
        if ('undefined' !== $today_since_id = $this->tokenStorage->getToken()->getUser()->getTodaySinceId()) {
            $get_query['since_id'] = $today_since_id;
        // since_idがundefinedなら200件まで取得 今日以前のつぶやきが存在しないアカウントなど
        } else {
            $get_query['count'] = '200';
        }
        $timeline = $this->callStatusesUserTimeline($get_query);

        return $timeline;
    }

    /**
     * search since_id & set db. return today timeline json
     *
     * @param array $get_query api parameters
     * @return array|null timeline or null
     */
    protected function searchTodayTimeline(array $get_query)
    {
        $today = (new \DateTime())->format('Y-m-d');
        $saved_timelime = array(); // 今までに取得したtimelime
        $index = 0; // for文を回した回数
<commit_msg>[TwetterAPI.php] rename method fit the doctorine.<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;
use Doctrine\Bundle\DoctrineBundle\Registry;

class TwitterAPI
{
    /**
    * @var Doctrine\Bundle\DoctrineBundle\Registry
    */
    protected $doctrine;

    /**
    * @var Symfony\Component\Security\Core\Authentication\Token\Storage
    */
    protected $tokenStorage;

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

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

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

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

    /**
     * get today timeline json
     *
     * @return array|null timeline or null
     */
    public function getTodayTimeline()
    {
        $get_query = ['user_id' => $this->tokenStorage->getToken()->getRawToken()['user_id']];
        $today = (new \DateTime())->format('Y-m-d');
        $since_id_at = $this->tokenStorage->getToken()->getUser()->getSinceIdAt();

        // 今日の始点ツイートのsince_idが無ければタイムラインをまるまる取得してsince_idを計算する
        if ($since_id_at === null || $since_id_at->format('Y-m-d') !== $today) {
            $timeline = $this->findTodayTimeline($get_query);

            return $timeline;
        }
        // since_idがあればget_queryに指定して今日のつぶやき一覧をapiから取得
        if ('undefined' !== $today_since_id = $this->tokenStorage->getToken()->getUser()->getTodaySinceId()) {
            $get_query['since_id'] = $today_since_id;
        // since_idがundefinedなら200件まで取得 今日以前のつぶやきが存在しないアカウントなど
        } else {
            $get_query['count'] = '200';
        }
        $timeline = $this->callStatusesUserTimeline($get_query);

        return $timeline;
    }

    /**
     * find since_id & set db. return today timeline json
     *
     * @param array $get_query api parameters
     * @return array|null timeline or null
     */
    protected function findTodayTimeline(array $get_query)
    {
        $today = (new \DateTime())->format('Y-m-d');
        $saved_timelime = array(); // 今までに取得したtimelime
        $index = 0; // for文を回した回数

{'fraction_non_alphanumeric': 0.1338375534363696, 'fraction_numerical': 0.0023018743834265043, 'mean_word_length': 3.08255033557047, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, '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': '6327952', 'n_tokens_mistral': 1997, 'n_tokens_neox': 1903, 'n_words': 333}