Ejemplo n.º 1
0
 /**
  * @expectedException \Mcfedr\TwitterPushBundle\Exception\MaxPushesPerHourException
  */
 public function testPushTweetRate()
 {
     $arn = 'topic';
     $messagesMock = $this->getMockBuilder(Messages::class)->disableOriginalConstructor()->setMethods(['send'])->getMock();
     $messagesMock->expects($this->exactly(5))->method('send')->with($this->isInstanceOf(Message::class), $this->equalTo($arn));
     $service = new TweetPusher($messagesMock, $arn, null, null, 5, new ArrayCache());
     for ($i = 0; $i < 6; $i++) {
         $service->pushTweet($this->getTweet());
     }
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->logger->info("Opening twitter stream to follow user {$this->userid}");
     $response = $this->client->post('statuses/filter.json', ['form_params' => ['follow' => $this->userid], 'stream' => true]);
     $stream = $response->getBody();
     // Read until the stream is closed
     $this->logger->info('Reading stream');
     $line = '';
     while (!$stream->eof()) {
         $line .= $stream->read(static::BLOCK_SIZE);
         while (strstr($line, "\r\n") !== false) {
             list($json, $line) = explode("\r\n", $line, 2);
             $this->logger->debug('Got a line', ['line' => $json]);
             if (trim($json) == '') {
                 $this->logger->debug('Keep alive');
                 continue;
             }
             $data = json_decode($json, true);
             if (isset($data['text'])) {
                 $this->logger->info('Received tweet', ['tweet' => $data]);
                 //Filter replies and retweets
                 if ($data['user']['id_str'] == $this->userid) {
                     try {
                         $this->pusher->pushTweet($data);
                         $this->logger->notice('Sent tweet', ['TweetId' => $data['id_str']]);
                     } catch (\Exception $e) {
                         $this->logger->error("Failed to push", ['TweetId' => $data['id_str'], 'Exception' => $e]);
                     }
                 } else {
                     $this->logger->info('Ignored tweet', ['TweetId' => $data['id_str']]);
                 }
             } else {
                 $this->logger->debug('Other message', ['message' => $data]);
             }
         }
     }
     $this->logger->info('Stream finished');
 }