ListCommand.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\FilesReminders\Command;
  8. use DateTimeInterface;
  9. use OC\Core\Command\Base;
  10. use OCA\FilesReminders\Model\RichReminder;
  11. use OCA\FilesReminders\Service\ReminderService;
  12. use OCP\IUserManager;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Style\SymfonyStyle;
  18. class ListCommand extends Base {
  19. public function __construct(
  20. private ReminderService $reminderService,
  21. private IUserManager $userManager,
  22. ) {
  23. parent::__construct();
  24. }
  25. protected function configure(): void {
  26. $this
  27. ->setName('files:reminders')
  28. ->setDescription('List file reminders')
  29. ->addArgument(
  30. 'user',
  31. InputArgument::OPTIONAL,
  32. 'list reminders for user',
  33. )
  34. ->addOption(
  35. 'output',
  36. null,
  37. InputOption::VALUE_OPTIONAL,
  38. 'Output format (plain, json or json_pretty, default is plain)',
  39. $this->defaultOutputFormat,
  40. );
  41. }
  42. protected function execute(InputInterface $input, OutputInterface $output): int {
  43. $io = new SymfonyStyle($input, $output);
  44. $uid = $input->getArgument('user');
  45. if ($uid !== null) {
  46. /** @var string $uid */
  47. $user = $this->userManager->get($uid);
  48. if ($user === null) {
  49. $io->error("Unknown user <$uid>");
  50. return 1;
  51. }
  52. }
  53. $reminders = $this->reminderService->getAll($user ?? null);
  54. $outputOption = $input->getOption('output');
  55. switch ($outputOption) {
  56. case static::OUTPUT_FORMAT_JSON:
  57. case static::OUTPUT_FORMAT_JSON_PRETTY:
  58. $this->writeArrayInOutputFormat(
  59. $input,
  60. $io,
  61. array_map(
  62. fn (RichReminder $reminder) => $reminder->jsonSerialize(),
  63. $reminders,
  64. ),
  65. '',
  66. );
  67. return 0;
  68. default:
  69. if (empty($reminders)) {
  70. $io->text('No reminders');
  71. return 0;
  72. }
  73. $io->table(
  74. ['User Id', 'File Id', 'Path', 'Due Date', 'Updated At', 'Created At', 'Notified'],
  75. array_map(
  76. fn (RichReminder $reminder) => [
  77. $reminder->getUserId(),
  78. $reminder->getFileId(),
  79. $reminder->getNode()->getPath(),
  80. $reminder->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601
  81. $reminder->getUpdatedAt()->format(DateTimeInterface::ATOM), // ISO 8601
  82. $reminder->getCreatedAt()->format(DateTimeInterface::ATOM), // ISO 8601
  83. $reminder->getNotified() ? 'true' : 'false',
  84. ],
  85. $reminders,
  86. ),
  87. );
  88. return 0;
  89. }
  90. }
  91. }