|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Command; |
| 6 | + |
| 7 | +use App\Entity\Task; |
| 8 | +use App\Repository\TaskRepository; |
| 9 | +use Symfony\Component\Console\Command\Command; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Input\InputOption; |
| 12 | +use Symfony\Component\Console\Output\OutputInterface; |
| 13 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author Tobias Nyholm <[email protected]> |
| 17 | + */ |
| 18 | +class ListTaskCommand extends Command |
| 19 | +{ |
| 20 | + protected static $defaultName = 'app:task:list'; |
| 21 | + private $repository; |
| 22 | + |
| 23 | + public function __construct(TaskRepository $repository) |
| 24 | + { |
| 25 | + parent::__construct(); |
| 26 | + $this->repository = $repository; |
| 27 | + } |
| 28 | + |
| 29 | + protected function configure() |
| 30 | + { |
| 31 | + $this->addOption('number', null, InputOption::VALUE_REQUIRED, 'The issue number we are interested in', null); |
| 32 | + } |
| 33 | + |
| 34 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 35 | + { |
| 36 | + /** @var int|null $number */ |
| 37 | + $number = $input->getOption('number'); |
| 38 | + if (null === $number) { |
| 39 | + $criteria = []; |
| 40 | + } else { |
| 41 | + $criteria = ['number' => $number]; |
| 42 | + } |
| 43 | + |
| 44 | + $limit = 100; |
| 45 | + $tasks = $this->repository->findBy($criteria, ['verifyAfter' => 'ASC'], $limit); |
| 46 | + $rows = []; |
| 47 | + foreach ($tasks as $task) { |
| 48 | + $rows[] = [ |
| 49 | + $task->getRepositoryFullName(), |
| 50 | + $task->getNumber(), |
| 51 | + $this->actionToString($task->getAction()), |
| 52 | + $task->getVerifyAfter()->format('Y-m-d H:i:s'), |
| 53 | + ]; |
| 54 | + } |
| 55 | + |
| 56 | + $io = new SymfonyStyle($input, $output); |
| 57 | + $io->table(['Repo', 'Number', 'Action', 'Verify After'], $rows); |
| 58 | + $io->write(sprintf('Total of %d items in the table. ', $taskCount = count($tasks))); |
| 59 | + if ($limit === $taskCount) { |
| 60 | + $io->write('There might be more tasks in the database.'); |
| 61 | + } |
| 62 | + $io->newLine(); |
| 63 | + |
| 64 | + return 0; |
| 65 | + } |
| 66 | + |
| 67 | + private function actionToString(int $action): string |
| 68 | + { |
| 69 | + $data = [ |
| 70 | + Task::ACTION_CLOSE_STALE => 'Close stale', |
| 71 | + Task::ACTION_CLOSE_DRAFT => 'Close draft', |
| 72 | + Task::ACTION_INFORM_CLOSE_STALE => 'Inform about close', |
| 73 | + ]; |
| 74 | + |
| 75 | + return $data[$action] ?? 'Unknown action'; |
| 76 | + } |
| 77 | +} |
0 commit comments