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

PublishCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
     * {@inheritdoc}
30
     */
31
    protected function configure()
32
    {
33
        $this
34
            ->setName('amqp:publish')
35
            ->setDescription('Send a message on a AMQP (RabbitMQ) bus.')
36
            ->setHelp('Reads a message from STDIN and sends it on the bus to the given exchange.')
37
            ->addArgument('exchange', InputArgument::REQUIRED, 'the target exchange')
38
            ->addArgument('filename', InputArgument::OPTIONAL, 'the file to send on the bus (if not passed, data is read from STDIN)');
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        $channel = $this->client->getChannel();
47
48
        $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...
49
        if ($filename = $input->getArgument('filename')) {
50
            $contents = file_get_contents($filename);
51
        } elseif (0 === ftell(STDIN)) {
52
            $contents = '';
53
            while (!feof(STDIN)) {
54
                $contents .= fread(STDIN, 1024);
55
            }
56
        } else {
57
            throw new \RuntimeException('Please provide a filename or pipe message to STDIN.');
58
        }
59
60
        $routingKey = 'key';
61
        $mandatory = false;
62
        $immediate = false;
63
        $ticket = null;
64
65
        $channel->basic_publish((new Message($contents))->toAMQPMessage(), $input->getArgument('exchange'), $routingKey, $mandatory, $immediate, $ticket);
66
    }
67
}
68