PutCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Tarantool\JobQueue\Console\Command;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class PutCommand extends Command
10
{
11
    protected function configure(): void
12
    {
13
        parent::configure();
14
15
        $this
16
            ->setName('put')
17
            ->setDescription('Puts a task into the queue')
18
            ->addArgument('json-data', InputArgument::REQUIRED)
19
        ;
20
    }
21
22
    protected function execute(InputInterface $input, OutputInterface $output): void
23
    {
24
        $json = $input->getArgument('json-data');
25
        $data = json_decode($json, true);
0 ignored issues
show
Bug introduced by
It seems like $json can also be of type string[]; however, parameter $json of json_decode() does only seem to accept 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

25
        $data = json_decode(/** @scrutinizer ignore-type */ $json, true);
Loading history...
26
27
        if (!is_array($data)) {
28
            throw new \InvalidArgumentException('Invalid json data.');
29
        }
30
31
        $queue = $this->createConfigFactory($input, $output)->createQueue();
32
        $task = $queue->put($data);
33
34
        $output->writeln(sprintf(
35
            '<comment>%s</comment> was successfully put to <info>%s</info> (#<comment>%d</comment>).',
36
            json_encode($task->getData()),
37
            $queue->getName(),
38
            $task->getId()
39
        ));
40
    }
41
}
42