FailedMessagesRemoveCommand.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Messenger\Command;
  11. use Symfony\Component\Console\Exception\RuntimeException;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Style\SymfonyStyle;
  18. use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
  19. use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
  20. /**
  21. * @author Ryan Weaver <ryan@symfonycasts.com>
  22. */
  23. class FailedMessagesRemoveCommand extends AbstractFailedMessagesCommand
  24. {
  25. protected static $defaultName = 'messenger:failed:remove';
  26. /**
  27. * {@inheritdoc}
  28. */
  29. protected function configure(): void
  30. {
  31. $this
  32. ->setDefinition([
  33. new InputArgument('id', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Specific message id(s) to remove'),
  34. new InputOption('force', null, InputOption::VALUE_NONE, 'Force the operation without confirmation'),
  35. new InputOption('show-messages', null, InputOption::VALUE_NONE, 'Display messages before removing it (if multiple ids are given)'),
  36. ])
  37. ->setDescription('Remove given messages from the failure transport')
  38. ->setHelp(<<<'EOF'
  39. The <info>%command.name%</info> removes given messages that are pending in the failure transport.
  40. <info>php %command.full_name% {id1} [{id2} ...]</info>
  41. The specific ids can be found via the messenger:failed:show command.
  42. EOF
  43. )
  44. ;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function execute(InputInterface $input, OutputInterface $output)
  50. {
  51. $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
  52. $receiver = $this->getReceiver();
  53. $shouldForce = $input->getOption('force');
  54. $ids = (array) $input->getArgument('id');
  55. $shouldDisplayMessages = $input->getOption('show-messages') || 1 === \count($ids);
  56. $this->removeMessages($ids, $receiver, $io, $shouldForce, $shouldDisplayMessages);
  57. return 0;
  58. }
  59. private function removeMessages(array $ids, ReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce, bool $shouldDisplayMessages): void
  60. {
  61. if (!$receiver instanceof ListableReceiverInterface) {
  62. throw new RuntimeException(sprintf('The "%s" receiver does not support removing specific messages.', $this->getReceiverName()));
  63. }
  64. foreach ($ids as $id) {
  65. $envelope = $receiver->find($id);
  66. if (null === $envelope) {
  67. $io->error(sprintf('The message with id "%s" was not found.', $id));
  68. continue;
  69. }
  70. if ($shouldDisplayMessages) {
  71. $this->displaySingleMessage($envelope, $io);
  72. }
  73. if ($shouldForce || $io->confirm('Do you want to permanently remove this message?', false)) {
  74. $receiver->reject($envelope);
  75. $io->success(sprintf('Message with id %s removed.', $id));
  76. } else {
  77. $io->note(sprintf('Message with id %s not removed.', $id));
  78. }
  79. }
  80. }
  81. }