Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
38 / 38
AlexisLefebvre\Bundle\AsyncTweetsBundle\Command\StatusesReadCommand
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
38 / 38
 configure
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
5 / 5
 execute
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
12 / 12
 displayTweets
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
10 / 10
 setTable
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
7 / 7
 formatCell
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
<?php
namespace AlexisLefebvre\Bundle\AsyncTweetsBundle\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class StatusesReadCommand extends BaseCommand
{
    private $table;
    protected function configure()
    {
        parent::configure();
        $this
            ->setName('statuses:read')
            ->setDescription('Read home timeline')
            ->addArgument('page', InputArgument::OPTIONAL, 'Page');
    }
    /**
     * @param InputInterface  $input
     * @param OutputInterface $output
     *
     * @return int|void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $page = $input->getArgument('page');
        if ($page < 1) {
            $page = 1;
        }
        $output->writeln('Current page: <comment>'.$page.'</comment>');
        // Get the tweets
        $tweets = $this->em
            ->getRepository('AsyncTweetsBundle:Tweet')
            ->getWithUsers($page);
        if (!$tweets) {
            $output->writeln('<info>No tweet to display.</info>');
            return 0;
        }
        $this->displayTweets($output, $tweets);
    }
    /**
     * @param OutputInterface $output
     * @param array           $tweets
     */
    protected function displayTweets(OutputInterface $output,
        $tweets)
    {
        $this->setTable($output);
        foreach ($tweets as $tweet) {
            $this->table->addRows([
                [
                    $this->formatCell('info',
                        $tweet->getUser()->getName(), 13),
                    $this->formatCell('comment',
                        $tweet->getText(), 40),
                    $tweet->getCreatedAt()->format('Y-m-d H:i'),
                ],
                // empty row between tweets
                ['', '', ''], ]
            );
        }
        $this->table->render();
    }
    protected function setTable(OutputInterface $output)
    {
        $this->table = new Table($output);
        $this->table
            ->setHeaders([
                // Add spaces to use all the 80 columns,
                //  even if name or texts are short
                sprintf('%-13s', 'Name'),
                sprintf('%-40s', 'Text'),
                sprintf('%-16s', 'Datetime'),
            ]);
    }
    /**
     * @param string $tag
     * @param string $content
     * @param int    $length
     *
     * @return string
     */
    protected function formatCell($tag, $content, $length)
    {
        return '<'.$tag.'>'.
            // Close and reopen the tag before each new line
            str_replace("\n", '</'.$tag.">\n<".$tag.'>',
                wordwrap($content, $length, "\n")
            ).
            '</'.$tag.'>';
    }
}