|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ReputationVIP\Bundle\QueueClientBundle\Command; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | +use Psr\Log\LoggerInterface; |
| 7 | +use ReputationVIP\Bundle\QueueClientBundle\Configuration\QueuesConfiguration; |
| 8 | +use ReputationVIP\Bundle\QueueClientBundle\Utils\Output; |
| 9 | +use ReputationVIP\QueueClient\QueueClientInterface; |
| 10 | +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
| 11 | +use Symfony\Component\Config\Definition\Processor; |
| 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\OutputInterface; |
| 16 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 17 | +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
| 18 | +use Symfony\Component\Yaml\Yaml; |
| 19 | + |
| 20 | +class DeleteQueuesCommand extends ContainerAwareCommand |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var Output $output |
| 24 | + */ |
| 25 | + private $output; |
| 26 | + |
| 27 | + protected function configure() |
| 28 | + { |
| 29 | + $this |
| 30 | + ->setName('queue-client:delete-queues') |
| 31 | + ->setDescription('Delete queues') |
| 32 | + ->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'File to read') |
| 33 | + ->addOption('force', null, InputOption::VALUE_NONE, 'If set, the task will not ask for confirm delete') |
| 34 | + ->addArgument('queues', InputArgument::IS_ARRAY, 'queues to delete') |
| 35 | + ->setHelp(<<<HELP |
| 36 | +This command deletes queues. |
| 37 | +
|
| 38 | +Specify file in config file: |
| 39 | +queue_client: |
| 40 | + queues_file: path/to/file.yml |
| 41 | +
|
| 42 | +Or specify file with file option: |
| 43 | + --file=path/to/file.yml |
| 44 | +
|
| 45 | +Or list queues to delete: |
| 46 | + queue-client:delete-queues queue1 queue2 queue3 |
| 47 | +HELP |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param QueueClientInterface $queueClient |
| 53 | + * @param string $fileName |
| 54 | + * @return int |
| 55 | + */ |
| 56 | + private function deleteFromFile($queueClient, $fileName) |
| 57 | + { |
| 58 | + try { |
| 59 | + $processor = new Processor(); |
| 60 | + $configuration = new QueuesConfiguration(); |
| 61 | + $processedConfiguration = $processor->processConfiguration($configuration, Yaml::parse(file_get_contents($fileName))); |
| 62 | + |
| 63 | + } catch (\Exception $e) { |
| 64 | + $this->output->write($e->getMessage(), Output::CRITICAL); |
| 65 | + |
| 66 | + return 1; |
| 67 | + } |
| 68 | + array_walk_recursive($processedConfiguration, 'ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory::resolveParameters', $this->getContainer()); |
| 69 | + $this->output->write('Start delete queue.', Output::INFO); |
| 70 | + foreach ($processedConfiguration[QueuesConfiguration::QUEUES_NODE] as $queue) { |
| 71 | + $queueName = $queue[QueuesConfiguration::QUEUE_NAME_NODE]; |
| 72 | + try { |
| 73 | + $queueClient->deleteQueue($queueName); |
| 74 | + $this->output->write('Queue ' . $queueName . ' deleted.', Output::INFO); |
| 75 | + } catch (\Exception $e) { |
| 76 | + $this->output->write($e->getMessage(), Output::WARNING); |
| 77 | + } |
| 78 | + } |
| 79 | + $this->output->write('End delete queue.', Output::INFO); |
| 80 | + |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param InputInterface $input |
| 86 | + * @param OutputInterface $output |
| 87 | + * @return int |
| 88 | + */ |
| 89 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 90 | + { |
| 91 | + $helper = $this->getHelper('question'); |
| 92 | + $force = $input->getOption('force') ? true : false; |
| 93 | + try { |
| 94 | + /** @var LoggerInterface $logger */ |
| 95 | + $logger = $this->getContainer()->get('logger'); |
| 96 | + } catch (ServiceNotFoundException $e) { |
| 97 | + $logger = null; |
| 98 | + } |
| 99 | + $this->output = new Output($logger, $output); |
| 100 | + try { |
| 101 | + /** @var QueueClientInterface $queueClient */ |
| 102 | + $queueClient = $this->getContainer()->get('queue_client'); |
| 103 | + } catch (ServiceNotFoundException $e) { |
| 104 | + $this->output->write('No queue client service found.', Output::CRITICAL); |
| 105 | + |
| 106 | + return 1; |
| 107 | + } |
| 108 | + if ($input->getOption('file')) { |
| 109 | + $fileName = $input->getOption('file'); |
| 110 | + if (!($force || $helper->ask($input, $output, new ConfirmationQuestion('Delete queues in file "' . $fileName . '"?', false)))) { |
| 111 | + |
| 112 | + return 0; |
| 113 | + } |
| 114 | + |
| 115 | + return $this->deleteFromFile($queueClient, $fileName); |
| 116 | + } else { |
| 117 | + $queues = $input->getArgument('queues'); |
| 118 | + if (count($queues)) { |
| 119 | + if (!($force || $helper->ask($input, $output, new ConfirmationQuestion(implode("\n", $queues) . "\nDelete queues list above?" , false)))) { |
| 120 | + |
| 121 | + return 0; |
| 122 | + } |
| 123 | + foreach ($queues as $queue) { |
| 124 | + try { |
| 125 | + $queueClient->deleteQueue($queue); |
| 126 | + $this->output->write('Queue ' . $queue . ' deleted.', Output::INFO); |
| 127 | + } catch (\Exception $e) { |
| 128 | + $this->output->write($e->getMessage(), Output::WARNING); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return 0; |
| 133 | + } |
| 134 | + try { |
| 135 | + $fileName = $this->getContainer()->getParameter('queue_client.queues_file'); |
| 136 | + if (!($force || $helper->ask($input, $output, new ConfirmationQuestion('Delete queues in file "' . $fileName . '"?', false)))) { |
| 137 | + |
| 138 | + return 0; |
| 139 | + } |
| 140 | + |
| 141 | + return $this->deleteFromFile($queueClient, $fileName); |
| 142 | + } catch (InvalidArgumentException $e) { |
| 143 | + $this->output->write('No queue_client.queues_file parameter found.', Output::CRITICAL); |
| 144 | + |
| 145 | + return 1; |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments