RunCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Queue\Command;
6
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
use Yiisoft\Queue\QueueFactoryInterface;
13
14
final class RunCommand extends Command
15
{
16
    protected static $defaultName = 'queue:run';
17
    protected static $defaultDescription = 'Runs all the existing messages in the given queues. ' .
18
        'Exits once messages are over.';
19
20 2
    public function __construct(private QueueFactoryInterface $queueFactory, private array $channels)
21
    {
22 2
        parent::__construct();
23
    }
24
25 2
    public function configure(): void
26
    {
27 2
        $this->addArgument(
28 2
            'channel',
29 2
            InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
30 2
            'Queue channel name list to connect to.',
31 2
            $this->channels,
32 2
        )
33 2
            ->addOption(
34 2
                'maximum',
35 2
                'm',
36 2
                InputOption::VALUE_REQUIRED,
37 2
                'Maximum number of messages to process in each channel. Default is 0 (no limits).',
38 2
                0,
39 2
            )
40 2
            ->addUsage('[channel1 [channel2 [...]]] --maximum 100');
41
    }
42
43 1
    protected function execute(InputInterface $input, OutputInterface $output): int
44
    {
45
        /** @var string $channel */
46 1
        foreach ($input->getArgument('channel') as $channel) {
47 1
            $output->write("Processing channel $channel... ");
48 1
            $count = $this->queueFactory
49 1
                ->get($channel)
50 1
                ->run((int)$input->getOption('maximum'));
51
52 1
            $output->writeln("Messages processed: $count.");
53
        }
54
55 1
        return 0;
56
    }
57
}
58