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:
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:
33: ->addOption('table', null, InputOption::VALUE_NONE,
34: 'Display a table with tweets');
35: }
36:
37: 38: 39: 40: 41: 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: 68:
69: protected function setAndDisplayLastTweet(OutputInterface $output)
70: {
71:
72: $lastTweet = $this->em
73: ->getRepository('AsyncTweetsBundle:Tweet')
74: ->getLastTweet();
75:
76:
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: 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: 108: 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: 123: 124: 125: 126:
127: protected function addAndDisplayTweets(InputInterface $input,
128: OutputInterface $output, $content, $numberOfTweets)
129: {
130: $output->writeln('<comment>Number of tweets: '.$numberOfTweets.'</comment>');
131:
132:
133:
134:
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: 151: 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: 163: 164:
165: protected function setTable(InputInterface $input,
166: OutputInterface $output)
167: {
168: $this->displayTable = $input->getOption('table');
169:
170:
171: if ($this->displayTable) {
172: $this->table = new Table($output);
173: $this->table
174: ->setHeaders(['Datetime', 'Text excerpt', 'Name']);
175: }
176: }
177:
178: 179: 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: