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