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 Symfony\Component\Console\Helper\Table;
  6: use Symfony\Component\Console\Input\InputArgument;
  7: use Symfony\Component\Console\Input\InputInterface;
  8: use Symfony\Component\Console\Output\OutputInterface;
  9: 
 10: class StatusesReadCommand extends BaseCommand
 11: {
 12:     private $table;
 13: 
 14:     protected function configure()
 15:     {
 16:         parent::configure();
 17: 
 18:         $this
 19:             ->setName('statuses:read')
 20:             ->setDescription('Read home timeline')
 21:             ->addArgument('page', InputArgument::OPTIONAL, 'Page');
 22:     }
 23: 
 24:     /**
 25:      * @param InputInterface  $input
 26:      * @param OutputInterface $output
 27:      *
 28:      * @return int|void
 29:      */
 30:     protected function execute(InputInterface $input, OutputInterface $output)
 31:     {
 32:         $page = $input->getArgument('page');
 33: 
 34:         if ($page < 1) {
 35:             $page = 1;
 36:         }
 37: 
 38:         $output->writeln('Current page: <comment>'.$page.'</comment>');
 39: 
 40:         // Get the tweets
 41:         $tweets = $this->em
 42:             ->getRepository('AsyncTweetsBundle:Tweet')
 43:             ->getWithUsers($page);
 44: 
 45:         if (!$tweets) {
 46:             $output->writeln('<info>No tweet to display.</info>');
 47: 
 48:             return 0;
 49:         }
 50: 
 51:         $this->displayTweets($output, $tweets);
 52:     }
 53: 
 54:     /**
 55:      * @param OutputInterface $output
 56:      * @param array           $tweets
 57:      */
 58:     protected function displayTweets(OutputInterface $output,
 59:         $tweets)
 60:     {
 61:         $this->setTable($output);
 62: 
 63:         foreach ($tweets as $tweet) {
 64:             $this->table->addRows([
 65:                 [
 66:                     $this->formatCell('info',
 67:                         $tweet->getUser()->getName(), 13),
 68:                     $this->formatCell('comment',
 69:                         $tweet->getText(), 40),
 70:                     $tweet->getCreatedAt()->format('Y-m-d H:i'),
 71:                 ],
 72:                 // empty row between tweets
 73:                 ['', '', ''], ]
 74:             );
 75:         }
 76: 
 77:         $this->table->render();
 78:     }
 79: 
 80:     protected function setTable(OutputInterface $output)
 81:     {
 82:         $this->table = new Table($output);
 83:         $this->table
 84:             ->setHeaders([
 85:                 // Add spaces to use all the 80 columns,
 86:                 //  even if name or texts are short
 87:                 sprintf('%-13s', 'Name'),
 88:                 sprintf('%-40s', 'Text'),
 89:                 sprintf('%-16s', 'Datetime'),
 90:             ]);
 91:     }
 92: 
 93:     /**
 94:      * @param string $tag
 95:      * @param string $content
 96:      * @param int    $length
 97:      *
 98:      * @return string
 99:      */
100:     protected function formatCell($tag, $content, $length)
101:     {
102:         return '<'.$tag.'>'.
103:             // Close and reopen the tag before each new line
104:             str_replace("\n", '</'.$tag.">\n<".$tag.'>',
105:                 wordwrap($content, $length, "\n")
106:             ).
107:             '</'.$tag.'>';
108:     }
109: }
110: 
AsyncTweetsBundle API documentation generated by ApiGen