1 | <?php |
||
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 |
|
104 | |||
105 | 1 | protected function formatCell(string $tag, string $content, int $length): string |
|
106 | { |
||
107 | 1 | return '<'.$tag.'>'. |
|
116 | } |
||
117 |