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: 26: 27: 28: 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:
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: 56: 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:
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:
86:
87: sprintf('%-13s', 'Name'),
88: sprintf('%-40s', 'Text'),
89: sprintf('%-16s', 'Datetime'),
90: ]);
91: }
92:
93: 94: 95: 96: 97: 98: 99:
100: protected function formatCell($tag, $content, $length)
101: {
102: return '<'.$tag.'>'.
103:
104: str_replace("\n", '</'.$tag.">\n<".$tag.'>',
105: wordwrap($content, $length, "\n")
106: ).
107: '</'.$tag.'>';
108: }
109: }
110: