ListCommand.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2023 Christopher Ng <chrng8@gmail.com>
  5. *
  6. * @author Christopher Ng <chrng8@gmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\FilesReminders\Command;
  25. use DateTimeInterface;
  26. use OC\Core\Command\Base;
  27. use OCA\FilesReminders\Model\RichReminder;
  28. use OCA\FilesReminders\Service\ReminderService;
  29. use OCP\IUserManager;
  30. use Symfony\Component\Console\Input\InputArgument;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Input\InputOption;
  33. use Symfony\Component\Console\Output\OutputInterface;
  34. use Symfony\Component\Console\Style\SymfonyStyle;
  35. class ListCommand extends Base {
  36. public function __construct(
  37. private ReminderService $reminderService,
  38. private IUserManager $userManager,
  39. ) {
  40. parent::__construct();
  41. }
  42. protected function configure(): void {
  43. $this
  44. ->setName('files:reminders')
  45. ->setDescription('List file reminders')
  46. ->addArgument(
  47. 'user',
  48. InputArgument::OPTIONAL,
  49. 'list reminders for user',
  50. )
  51. ->addOption(
  52. 'output',
  53. null,
  54. InputOption::VALUE_OPTIONAL,
  55. 'Output format (plain, json or json_pretty, default is plain)',
  56. $this->defaultOutputFormat,
  57. );
  58. }
  59. protected function execute(InputInterface $input, OutputInterface $output): int {
  60. $io = new SymfonyStyle($input, $output);
  61. $uid = $input->getArgument('user');
  62. if ($uid !== null) {
  63. /** @var string $uid */
  64. $user = $this->userManager->get($uid);
  65. if ($user === null) {
  66. $io->error("Unknown user <$uid>");
  67. return 1;
  68. }
  69. }
  70. $reminders = $this->reminderService->getAll($user ?? null);
  71. $outputOption = $input->getOption('output');
  72. switch ($outputOption) {
  73. case static::OUTPUT_FORMAT_JSON:
  74. case static::OUTPUT_FORMAT_JSON_PRETTY:
  75. $this->writeArrayInOutputFormat(
  76. $input,
  77. $io,
  78. array_map(
  79. fn (RichReminder $reminder) => $reminder->jsonSerialize(),
  80. $reminders,
  81. ),
  82. '',
  83. );
  84. return 0;
  85. default:
  86. if (empty($reminders)) {
  87. $io->text('No reminders');
  88. return 0;
  89. }
  90. $io->table(
  91. ['User Id', 'File Id', 'Path', 'Due Date', 'Updated At', 'Created At', 'Notified'],
  92. array_map(
  93. fn (RichReminder $reminder) => [
  94. $reminder->getUserId(),
  95. $reminder->getFileId(),
  96. $reminder->getNode()->getPath(),
  97. $reminder->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601
  98. $reminder->getUpdatedAt()->format(DateTimeInterface::ATOM), // ISO 8601
  99. $reminder->getCreatedAt()->format(DateTimeInterface::ATOM), // ISO 8601
  100. $reminder->getNotified() ? 'true' : 'false',
  101. ],
  102. $reminders,
  103. ),
  104. );
  105. return 0;
  106. }
  107. }
  108. }