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'); |
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.