QueueDestroyCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 19.79 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 5
dl 19
loc 96
ccs 0
cts 66
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A destroyQueue() 0 13 2
A setContainer() 0 4 1
A configure() 0 19 1
C execute() 19 38 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Console\Question\ConfirmationQuestion;
31
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
32
use Symfony\Component\DependencyInjection\ContainerInterface;
33
34
/**
35
 * @author Keith Kirk <[email protected]>
36
 */
37
class QueueDestroyCommand extends Command implements ContainerAwareInterface
38
{
39
    /**
40
     * @var ContainerInterface
41
     *
42
     * @api
43
     */
44
    protected $container;
45
46
    /**
47
     * Sets the Container associated with this Controller.
48
     *
49
     * @param ContainerInterface $container A ContainerInterface instance
50
     *
51
     * @api
52
     */
53
    public function setContainer(ContainerInterface $container = null)
54
    {
55
        $this->container = $container;
56
    }
57
58
    protected $output;
59
60
    protected function configure()
61
    {
62
        $this
63
            ->setName('uecode:qpush:destroy')
64
            ->setDescription('Destroys the configured Queues and cleans Cache')
65
            ->addArgument(
66
                'name',
67
                InputArgument::OPTIONAL,
68
                'Name of a specific queue to destroy',
69
                null
70
            )
71
            ->addOption(
72
                'force',
73
                null,
74
                InputOption::VALUE_NONE,
75
                'Set this parameter to force this action'
76
            )
77
        ;
78
    }
79
80
    protected function execute(InputInterface $input, OutputInterface $output)
81
    {
82
        $this->output = $output;
83
        $registry = $this->container->get('uecode_qpush');
84
        $questionHelper = $this->getHelperSet()->get('question');
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
                $question = new ConfirmationQuestion(sprintf(
90
                    '<comment>This will remove the %s queue, even if it has messages! Are you sure? </comment>',
91
                    $name
92
                ));
93
                $confirmation = $questionHelper->ask($input, $output, $question);
94
95
                if (!$confirmation) {
96
                    return 0;
97
                }
98
            }
99
100
            return $this->destroyQueue($registry, $name);
101
        }
102
103 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...
104
            $question = new ConfirmationQuestion('<comment>This will remove ALL queues, even if they have messages.  Are you sure? </comment>');
105
            $confirmation = $questionHelper->ask($input, $output, $question);
106
107
            if (!$confirmation) {
108
                return 0;
109
            }
110
        }
111
112
        foreach ($registry->all() as $queue) {
113
            $this->destroyQueue($registry, $queue->getName());
114
        }
115
116
        return 0;
117
    }
118
119
    private function destroyQueue($registry, $name)
120
    {
121
        if (!$registry->has($name)) {
122
            return $this->output->writeln(
123
                sprintf("The [%s] queue you have specified does not exists!", $name)
124
            );
125
        }
126
127
        $registry->get($name)->destroy();
128
        $this->output->writeln(sprintf("The %s queue has been successfully destroyed.", $name));
129
130
        return 0;
131
    }
132
}
133