QueueClearCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 71
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 16 1
B execute() 0 35 5
A getQueue() 0 4 1
1
<?php
2
3
namespace TreeHouse\QueueBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Helper\QuestionHelper;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\ConfirmationQuestion;
11
use TreeHouse\Queue\Amqp\QueueInterface;
12
13
class QueueClearCommand extends ContainerAwareCommand
14
{
15
    /**
16
     * @inheritdoc
17
     */
18
    protected function configure()
19
    {
20
        $this->setName('queue:clear');
21
        $this->addArgument('queue', InputArgument::REQUIRED, 'The name of the queue to purge');
22
        $this->setDescription('Purges all messages in a queue');
23
        $this->setHelp(<<<HELP
24
This utility command purges messages from a specified queue, by acknowledging
25
all messages. Note that this does not purge messages that have been retrieved
26
by consumers, but are unacknowledged (neither acked nor nacked). These messages
27
can be put back onto the queue be closing all connections (or restarting the
28
broker as a last resort).
29
30
<error>WARNING: this is a destructive operation and cannot be undone!</error>
31
HELP
32
        );
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        if ($input->isInteractive()) {
41
            $question = new ConfirmationQuestion(
42
                '<question>CAREFUL: this is a destructive operation, which cannot be undone! Are you sure you want to proceed? [n]</question> ',
43
                false
44
            );
45
46
            if (!(new QuestionHelper())->ask($input, $output, $question)) {
47
                return 0;
48
            }
49
        }
50
51
        $name = $input->getArgument('queue');
52
        $queue = $this->getQueue($name);
53
        $verbose = $output->getVerbosity() > $output::VERBOSITY_VERBOSE;
54
55
        $output->writeln(sprintf('Purging queue <info>%s</info>', $name));
56
57
        $purged = 0;
58
        while ($envelope = $queue->get()) {
59
            ++$purged;
60
61
            if ($verbose) {
62
                $output->writeln(sprintf('<option=bold;fg=red>- %s</option=bold;fg=red>', $envelope->getDeliveryTag()));
63
            } else {
64
                $output->write(str_pad(sprintf('Purged <info>%d</info> messages', $purged), 50, ' ', STR_PAD_RIGHT));
65
                $output->write("\x0D");
66
            }
67
        }
68
69
        $output->writeln(str_pad(sprintf('Purged <info>%d</info> messages', $purged), 50, ' ', STR_PAD_RIGHT));
70
71
        return 0;
72
    }
73
74
    /**
75
     * @param string $name
76
     *
77
     * @return QueueInterface
78
     */
79
    protected function getQueue($name)
80
    {
81
        return $this->getContainer()->get(sprintf('tree_house.queue.queue.%s', $name));
82
    }
83
}
84