Passed
Push — master ( f4b60f...a7ff9b )
by Brice
05:36
created

AddTask   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 48
ccs 16
cts 24
cp 0.6667
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
B execute() 0 26 3
1
<?php
2
3
namespace JobQueue\Application\Console;
4
5
use JobQueue\Application\Utils\CommandTrait;
6
use JobQueue\Domain\Task\Profile;
7
use JobQueue\Domain\Task\Task;
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
final class AddTask extends ManagerCommand
14
{
15
    use CommandTrait;
16
17
    public function configure()
18
    {
19
        $this
20
            ->setName('add')
21
            ->setDescription('Add a new task to the queue')
22
            ->addArgument('profile', InputArgument::REQUIRED, 'Profile name')
23
            ->addArgument('job', InputArgument::REQUIRED, 'Job class name')
24
            ->addArgument('parameters', InputArgument::IS_ARRAY, 'List of parameters (key:value)', [])
25
            ->addOption('tags', 't', InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, 'Add one or multiple (array) tags')
26
        ;
27
    }
28
29
    /**
30
     *
31
     * @param InputInterface $input
32
     * @param OutputInterface $output
33
     * @return int
34
     */
35 1
    protected function execute(InputInterface $input, OutputInterface $output): int
36
    {
37 1
        $jobName = $input->getArgument('job');
38
39 1
        $parameters = [];
40 1
        foreach ($input->getArgument('parameters') as $parameter) {
41 1
            list($name, $value) = explode(':', $parameter);
42 1
            $parameters[trim($name)] = trim($value);
43
        }
44
45 1
        $tags = [];
46 1
        foreach ($input->getOption('tags') as $tag) {
47 1
            $tags[] = trim($tag);
48
        }
49
50 1
        $task = new Task(
51 1
            new Profile($input->getArgument('profile')),
52 1
            new $jobName,
53 1
            $parameters, $tags
54
        );
55
56 1
        $this->queue->add($task);
57
58 1
        $this->formatTaskBlock($task, $output);
59
60 1
        return 0;
61
    }
62
}
63