Completed
Push — master ( 650d1d...ac8b27 )
by Keith
11:21
created

QueueDestroyCommand::execute()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 45
Code Lines 26

Duplication

Lines 25
Ratio 55.56 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 25
loc 45
rs 6.7272
ccs 0
cts 37
cp 0
cc 7
eloc 26
nc 8
nop 2
crap 56
1
<?php
2
3
/**
4
 * Copyright 2014 Underground Elephant
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 *
18
 * @package     qpush-bundle
19
 * @copyright   Underground Elephant 2014
20
 * @license     Apache License, Version 2.0
21
 */
22
23
namespace Uecode\Bundle\QPushBundle\Command;
24
25
use Symfony\Component\Console\Command\Command;
26
use Symfony\Component\Console\Input\InputArgument;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
31
use Symfony\Component\DependencyInjection\ContainerInterface;
32
33
/**
34
 * @author Keith Kirk <[email protected]>
35
 */
36
class QueueDestroyCommand extends Command implements ContainerAwareInterface
37
{
38
    /**
39
     * @var ContainerInterface
40
     *
41
     * @api
42
     */
43
    protected $container;
44
45
    /**
46
     * Sets the Container associated with this Controller.
47
     *
48
     * @param ContainerInterface $container A ContainerInterface instance
49
     *
50
     * @api
51
     */
52
    public function setContainer(ContainerInterface $container = null)
53
    {
54
        $this->container = $container;
55
    }
56
57
    protected $output;
58
59
    protected function configure()
60
    {
61
        $this
62
            ->setName('uecode:qpush:destroy')
63
            ->setDescription('Destroys the configured Queues and cleans Cache')
64
            ->addArgument(
65
                'name',
66
                InputArgument::OPTIONAL,
67
                'Name of a specific queue to destroy',
68
                null
69
            )
70
            ->addOption(
71
                'force',
72
                null,
73
                InputOption::VALUE_NONE,
74
                'Set this parameter to force this action'
75
            )
76
        ;
77
    }
78
79
    protected function execute(InputInterface $input, OutputInterface $output)
80
    {
81
        $this->output = $output;
82
        $registry = $this->container->get('uecode_qpush');
83
        $dialog = $this->getHelperSet()->get('dialog');
84
85
        $name = $input->getArgument('name');
86
87
        if (null !== $name) {
88 View Code Duplication
            if (!$input->getOption('force')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
                $response = $dialog->askConfirmation(
90
                    $output,
91
                    sprintf(
92
                        '<comment>This will remove the %s queue, even if it has messages! Are you sure? </comment>',
93
                        $name
94
                    ),
95
                    false
96
                );
97
98
                if (!$response) {
99
                    return 0;
100
                }
101
            }
102
103
            return $this->destroyQueue($registry, $name);
104
        }
105
106 View Code Duplication
        if (!$input->getOption('force')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
            $response = $dialog->askConfirmation(
108
                $output,
109
                '<comment>This will remove ALL queues, even if they have messages.  Are you sure? </comment>',
110
                false
111
            );
112
113
            if (!$response) {
114
                return 0;
115
            }
116
        }
117
118
        foreach ($registry->all() as $queue) {
119
            $this->destroyQueue($registry, $queue->getName());
120
        }
121
122
        return 0;
123
    }
124
125
    private function destroyQueue($registry, $name)
126
    {
127
        if (!$registry->has($name)) {
128
            return $this->output->writeln(
129
                sprintf("The [%s] queue you have specified does not exists!", $name)
130
            );
131
        }
132
133
        $registry->get($name)->destroy();
134
        $this->output->writeln(sprintf("The %s queue has been successfully destroyed.", $name));
135
136
        return 0;
137
    }
138
}
139