Passed
Push — master ( 9bb7f3...8346df )
by Koldo
02:28
created

StartQueueConsumer::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 10
cp 0
rs 10
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Queue\Cli;
6
7
use Enqueue\Consumption\QueueConsumerInterface;
8
use Enqueue\Consumption\Result;
9
use Interop\Queue\Context;
10
use Interop\Queue\Message;
11
use Interop\Queue\Processor;
12
use InvalidArgumentException;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class StartQueueConsumer extends Command
19
{
20
    public const NAME = 'queue:start';
21
    private QueueConsumerInterface $consumer;
22
    private Processor $processor;
23
    /** @var Context */
24
    private Context $context;
25
26 2
    public function __construct(QueueConsumerInterface $consumer, Processor $messageProcessor, Context $context)
27
    {
28 2
        $this->consumer = $consumer;
29 2
        $this->processor = $messageProcessor;
30 2
        $this->context = $context;
31 2
        parent::__construct();
32 2
    }
33
34 2
    protected function configure(): void
35
    {
36 2
        $this->setName(self::NAME)
37 2
            ->setDescription('Start listening to the given queue name.')
38 2
            ->addArgument(
39 2
                'queue_name',
40 2
                InputArgument::REQUIRED,
41 2
                'The queue name we want to consume'
42
            );
43 2
    }
44
45
    protected function execute(InputInterface $input, OutputInterface $output): int
46
    {
47
        $queue = $input->getArgument('queue_name');
48
        if (false === is_string($queue)) {
49
            throw new InvalidArgumentException('Argument "queue_name" must be of type string.');
50
        }
51
        $this->consumer->bindCallback(
52
            $queue,
0 ignored issues
show
Bug introduced by
It seems like $queue can also be of type string[]; however, parameter $queueName of Enqueue\Consumption\Queu...terface::bindCallback() does only seem to accept Interop\Queue\Queue|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
            /** @scrutinizer ignore-type */ $queue,
Loading history...
53
            fn(Message $message) => $this->processor->process($message, $this->context)
54
        );
55
56
        $this->consumer->consume();
57
58
        return 0;
59
    }
60
}
61