Completed
Push — develop ( 53c9e6...feab1b )
by Baptiste
07:33
created

ConsumeCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\AMQPBundle\Command;
5
6
use Innmind\AMQP\Model\Basic\Consume;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\{
9
    Input\InputInterface,
10
    Input\InputArgument,
11
    Output\OutputInterface
12
};
13
14
final class ConsumeCommand extends ContainerAwareCommand
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected function configure()
20
    {
21
        $this
22
            ->setName('innmind:amqp:consume')
23
            ->setDescription('Will process messages from the given queue')
24
            ->addArgument('queue', InputArgument::REQUIRED)
25
            ->addArgument('number', InputArgument::OPTIONAL, 'The number of messages to process');
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function execute(InputInterface $input, OutputInterface $output)
32
    {
33
        $queue = $input->getArgument('queue');
34
        $consume = $this
35
            ->getContainer()
36
            ->get('innmind.amqp.consumers')
37
            ->get($queue);
38
39
        $consumer = $this
40
            ->getContainer()
41
            ->get('innmind.amqp.client')
42
            ->channel()
43
            ->basic()
44
            ->consume(new Consume($queue));
45
46
        if ($input->getArgument('number')) {
47
            $consumer->take((int) $input->getArgument('number'));
48
        }
49
50
        $consumer->foreach($consume);
51
    }
52
}
53