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