PublishCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 5
dl 0
loc 60
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configure() 0 13 1
A execute() 0 23 4
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);
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('exchange') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, PhpAmqpLib\Channel\AMQPChannel::basic_publish() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
71
    }
72
}
73