Completed
Pull Request — master (#58)
by Alexis
02:59
created

StatusesReadCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 4
dl 0
loc 100
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0

5 Methods

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