AmqpDebugCommand   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 92
Duplicated Lines 18.48 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 17
loc 92
ccs 0
cts 77
cp 0
rs 10
c 2
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 17 17 1
A execute() 0 69 2

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
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * The software is based on the Axon Framework project which is
17
 * licensed under the Apache 2.0 license. For more information on the Axon Framework
18
 * see <http://www.axonframework.org/>.
19
 * 
20
 * This software consists of voluntary contributions made by many individuals
21
 * and is licensed under the MIT license. For more information, see
22
 * <http://www.governor-framework.org/>.
23
 */
24
25
namespace Governor\Bundle\GovernorBundle\Command;
26
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputArgument;
29
use Symfony\Component\Console\Output\OutputInterface;
30
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
31
use Governor\Framework\EventHandling\Io\EventMessageReader;
32
33
/**
34
 * Description of AmqpDebugCommand
35
 *
36
 * @author david
37
 */
38
class AmqpDebugCommand extends ContainerAwareCommand
39
{
40
41 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
42
    {
43
        $this
44
            ->setName('governor:amqp-debug')
45
            ->addArgument(
46
                "exchange",
47
                InputArgument::REQUIRED,
48
                "Name of the exchange to listen read from"
49
            )
50
            ->addArgument(
51
                "connection",
52
                InputArgument::OPTIONAL,
53
                "Name of the connection to use",
54
                "default"
55
            )
56
            ->setDescription('Displays events routed to the AMQP terminal.');
57
    }
58
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        $formatter = $this->getHelperSet()->get('formatter');
62
        $serializer = $this->getContainer()->get('governor.serializer');
63
        $connection = $this->getContainer()->get(
64
            sprintf(
65
                "governor.amqp_terminal.connection.%s",
66
                $input->getArgument("connection")
67
            )
68
        );
69
70
        $channel = $connection->channel();
71
72
        $output->writeln(
73
            $formatter->formatSection(
74
                'Connecting',
75
                'Using connection '.$input->getArgument('connection')
76
            )
77
        );
78
79
        list($queueName, ,) = $channel->queue_declare(
80
            "",
81
            false,
82
            false,
83
            true,
84
            false
85
        );
86
        $channel->queue_bind($queueName, $input->getArgument('exchange'), '#');
87
88
        $output->writeln(
89
            $formatter->formatSection(
90
                '*',
91
                'Waiting for events. To exit press CTRL+C'
92
            )
93
        );
94
95
        $callback = function ($msg) use ($output, $formatter, $serializer) {
96
            $reader = new EventMessageReader($serializer);
97
            $message = $reader->readEventMessage($msg->body);
98
99
            $output->writeln(
100
                $formatter->formatSection(
101
                    '*',
102
                    sprintf(
103
                        "Recieved event with routing key %s",
104
                        $msg->delivery_info['routing_key']
105
                    )
106
                )
107
            );
108
            $output->writeln(array(' [x] '.print_r($message, true)));
109
        };
110
111
        $channel->basic_consume(
112
            $queueName,
113
            '',
114
            false,
115
            true,
116
            false,
117
            false,
118
            $callback
119
        );
120
121
        while (count($channel->callbacks)) {
122
            $channel->wait();
123
        }
124
125
        $channel->close();
126
        $connection->close();
127
    }
128
129
}
130