Overview

Namespaces

  • AlexisLefebvre
    • Bundle
      • AsyncTweetsBundle
        • Command
        • Controller
        • Entity
        • Utils

Classes

  • AlexisLefebvre\Bundle\AsyncTweetsBundle\AsyncTweetsBundle
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\BaseCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\StatusesHomeTimelineCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\StatusesHomeTimelineTestCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\StatusesReadCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\StatusesShowCommand
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Controller\DefaultController
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\Media
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\Tweet
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\TweetRepository
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Entity\User
  • AlexisLefebvre\Bundle\AsyncTweetsBundle\Utils\PersistTweet
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace AlexisLefebvre\Bundle\AsyncTweetsBundle\Command;
  4: 
  5: use Abraham\TwitterOAuth\TwitterOAuth;
  6: use AlexisLefebvre\Bundle\AsyncTweetsBundle\Utils\PersistTweet;
  7: use Symfony\Component\Console\Helper\ProgressBar;
  8: use Symfony\Component\Console\Helper\Table;
  9: use Symfony\Component\Console\Input\InputInterface;
 10: use Symfony\Component\Console\Input\InputOption;
 11: use Symfony\Component\Console\Output\OutputInterface;
 12: 
 13: class StatusesHomeTimelineCommand extends BaseCommand
 14: {
 15:     private $displayTable;
 16:     private $table;
 17:     private $progress;
 18: 
 19:     /** @see https://dev.twitter.com/rest/reference/get/statuses/home_timeline */
 20:     private $parameters = [
 21:         'count'           => 200,
 22:         'exclude_replies' => true,
 23:     ];
 24: 
 25:     protected function configure()
 26:     {
 27:         parent::configure();
 28: 
 29:         $this
 30:             ->setName('statuses:hometimeline')
 31:             ->setDescription('Fetch home timeline')
 32:             // http://symfony.com/doc/2.3/cookbook/console/console_command.html#automatically-registering-commands
 33:             ->addOption('table', null, InputOption::VALUE_NONE,
 34:                 'Display a table with tweets');
 35:     }
 36: 
 37:     /**
 38:      * @param InputInterface  $input
 39:      * @param OutputInterface $output
 40:      *
 41:      * @return int|void
 42:      */
 43:     protected function execute(InputInterface $input, OutputInterface $output)
 44:     {
 45:         $this->setAndDisplayLastTweet($output);
 46: 
 47:         $content = $this->getContent($input);
 48: 
 49:         if (!is_array($content)) {
 50:             $this->displayContentNotArrayError($output, $content);
 51: 
 52:             return 1;
 53:         }
 54: 
 55:         $numberOfTweets = count($content);
 56: 
 57:         if ($numberOfTweets == 0) {
 58:             $output->writeln('<comment>No new tweet.</comment>');
 59: 
 60:             return 0;
 61:         }
 62: 
 63:         $this->addAndDisplayTweets($input, $output, $content, $numberOfTweets);
 64:     }
 65: 
 66:     /**
 67:      * @param OutputInterface $output
 68:      */
 69:     protected function setAndDisplayLastTweet(OutputInterface $output)
 70:     {
 71:         // Get the last tweet
 72:         $lastTweet = $this->em
 73:             ->getRepository('AsyncTweetsBundle:Tweet')
 74:             ->getLastTweet();
 75: 
 76:         // And use it in the request if it exists
 77:         if ($lastTweet) {
 78:             $this->parameters['since_id'] = $lastTweet->getId();
 79: 
 80:             $comment = 'last tweet = '.$this->parameters['since_id'];
 81:         } else {
 82:             $comment = 'no last tweet';
 83:         }
 84: 
 85:         $output->writeln('<comment>'.$comment.'</comment>');
 86:     }
 87: 
 88:     /**
 89:      * @param InputInterface $input
 90:      */
 91:     protected function getContent(InputInterface $input)
 92:     {
 93:         $connection = new TwitterOAuth(
 94:             $this->container->getParameter('twitter_consumer_key'),
 95:             $this->container->getParameter('twitter_consumer_secret'),
 96:             $this->container->getParameter('twitter_token'),
 97:             $this->container->getParameter('twitter_token_secret')
 98:         );
 99: 
100:         return $connection->get(
101:             'statuses/home_timeline',
102:             $this->parameters
103:         );
104:     }
105: 
106:     /**
107:      * @param OutputInterface $output
108:      * @param null|object     $content
109:      */
110:     protected function displayContentNotArrayError(OutputInterface $output,
111:         $content)
112:     {
113:         $formatter = $this->getHelper('formatter');
114: 
115:         $errorMessages = ['Error!', 'Something went wrong, $content is not an array.'];
116:         $formattedBlock = $formatter->formatBlock($errorMessages, 'error');
117:         $output->writeln($formattedBlock);
118:         $output->writeln(print_r($content, true));
119:     }
120: 
121:     /**
122:      * @param InputInterface  $input
123:      * @param OutputInterface $output
124:      * @param array           $content
125:      * @param int             $numberOfTweets
126:      */
127:     protected function addAndDisplayTweets(InputInterface $input,
128:         OutputInterface $output, $content, $numberOfTweets)
129:     {
130:         $output->writeln('<comment>Number of tweets: '.$numberOfTweets.'</comment>');
131: 
132:         // Iterate through $content in order to add the oldest tweet first,
133:         //  if there is an error the oldest tweet will still be saved
134:         //  and newer tweets can be saved next time the command is launched
135:         $tweets = array_reverse($content);
136: 
137:         $this->setProgressBar($output, $numberOfTweets);
138:         $this->setTable($input, $output);
139:         $this->iterateTweets($tweets);
140: 
141:         $this->progress->finish();
142:         $output->writeln('');
143: 
144:         if ($this->displayTable) {
145:             $this->table->render();
146:         }
147:     }
148: 
149:     /**
150:      * @param OutputInterface $output
151:      * @param int             $numberOfTweets
152:      */
153:     protected function setProgressBar(OutputInterface $output,
154:         $numberOfTweets)
155:     {
156:         $this->progress = new ProgressBar($output, $numberOfTweets);
157:         $this->progress->setBarCharacter('<comment>=</comment>');
158:         $this->progress->start();
159:     }
160: 
161:     /**
162:      * @param InputInterface  $input
163:      * @param OutputInterface $output
164:      */
165:     protected function setTable(InputInterface $input,
166:         OutputInterface $output)
167:     {
168:         $this->displayTable = $input->getOption('table');
169: 
170:         // Display
171:         if ($this->displayTable) {
172:             $this->table = new Table($output);
173:             $this->table
174:                 ->setHeaders(['Datetime', 'Text excerpt', 'Name']);
175:         }
176:     }
177: 
178:     /**
179:      * @param array $tweets
180:      */
181:     protected function iterateTweets($tweets)
182:     {
183:         $persistTweet = new PersistTweet($this->em, $this->displayTable,
184:             $this->table);
185: 
186:         foreach ($tweets as $tweetTmp) {
187:             $persistTweet->addTweet($tweetTmp, true);
188: 
189:             $this->progress->advance();
190:         }
191:     }
192: }
193: 
AsyncTweetsBundle API documentation generated by ApiGen