Completed
Pull Request — 1.0 (#2)
by Jérémie
02:27
created

PublishCommand::execute()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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\Output\OutputInterface;
11
12
class PublishCommand extends Command
13
{
14
    /**
15
     * @var Client
16
     */
17
    private $client;
18
19
    /**
20
     * @param Client $client
21
     */
22
    public function __construct(Client $client)
23
    {
24
        $this->client = $client;
25
        parent::__construct();
26
    }
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
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $channel = $this->client->getChannel();
48
49
        $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...
50
        if ($filename = $input->getArgument('filename')) {
51
            $contents = file_get_contents($filename);
52
        } else if (0 === ftell(STDIN)) {
53
            $contents = '';
54
            while (!feof(STDIN)) {
55
                $contents .= fread(STDIN, 1024);
56
            }
57
        } else {
58
            throw new \RuntimeException("Please provide a filename or pipe message to STDIN.");
59
        }
60
61
        $routingKey = 'key';
62
        $mandatory = false;
63
        $immediate = false;
64
        $ticket = null;
65
66
        $channel->basic_publish((new Message($contents))->toAMQPMessage(), $input->getArgument('exchange'), $routingKey, $mandatory, $immediate, $ticket);
67
    }
68
69
}