Completed
Push — 1.0 ( c155d9...973d0d )
by David
03:18
created

PublishCommand::execute()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 23
rs 8.7972
cc 4
eloc 16
nc 3
nop 2
1
<?php
2
3
namespace Mouf\AmqpClient\Commands;
4
5
use Mouf\AmqpClient\Client;
6
use Mouf\AmqpClient\Objects\Message;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class PublishCommand extends Command
14
{
15
    /**
16
     * @var Client
17
     */
18
    private $client;
19
20
    /**
21
     * @param Client $client
22
     */
23
    public function __construct(Client $client)
24
    {
25
        $this->client = $client;
26
        parent::__construct();
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('amqp:publish')
36
            ->setDescription('Send a message on a AMQP (RabbitMQ) bus.')
37
            ->setHelp('Reads a message from STDIN and sends it on the bus to the given exchange.')
38
            ->addArgument('exchange', InputArgument::REQUIRED, 'the target exchange')
39
            ->addArgument('filename', InputArgument::OPTIONAL, 'the file to send on the bus (if not passed, data is read from STDIN)')
40
            ->addOption('routing_key', 'k', InputOption::VALUE_REQUIRED, 'the routing key of the message', 'key')
41
            ->addOption('mandatory', 'm', InputOption::VALUE_NONE, 'set the mandatory bit on the AMQP message')
42
            ->addOption('immediate', 'i', InputOption::VALUE_NONE, 'set the immediate bit on the AMQP message')
43
            ->addOption('ticket', 't', InputOption::VALUE_REQUIRED, 'set the ticket number');
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $channel = $this->client->getChannel();
52
53
        $filename = $input->getArgument('filename');
0 ignored issues
show
Unused Code introduced by
$filename is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
54
        if ($filename = $input->getArgument('filename')) {
55
            $contents = file_get_contents($filename);
56
        } elseif (0 === ftell(STDIN)) {
57
            $contents = '';
58
            while (!feof(STDIN)) {
59
                $contents .= fread(STDIN, 1024);
60
            }
61
        } else {
62
            throw new \RuntimeException('Please provide a filename or pipe message to STDIN.');
63
        }
64
65
        $routingKey = $input->getOption('routing_key');
66
        $mandatory = $input->getOption('mandatory');
67
        $immediate = $input->getOption('immediate');
68
        $ticket = $input->getOption('ticket');
69
70
        $channel->basic_publish((new Message($contents))->toAMQPMessage(), $input->getArgument('exchange'), $routingKey, $mandatory, $immediate, $ticket);
71
    }
72
}
73