Passed
Push — v2 ( 9bff46...d4d6f5 )
by Brice
03:06
created

AddTask::execute()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
ccs 18
cts 18
cp 1
cc 3
eloc 17
nc 4
nop 2
crap 3
1
<?php
2
3
namespace JobQueue\Application\Manager;
4
5
use JobQueue\Application\Utils\CommandTrait;
6
use JobQueue\Domain\Task\Profile;
7
use JobQueue\Domain\Task\Task;
8
use JobQueue\Infrastructure\ServiceContainer;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
final class AddTask extends Command
16
{
17
    use CommandTrait;
18
19
    public function configure()
20
    {
21
        $this
22
            ->setName('add')
23
            ->setDescription('Add a new task to the queue')
24
            ->addArgument('profile', InputArgument::REQUIRED, 'Profile name')
25
            ->addArgument('job', InputArgument::REQUIRED, 'Job class name')
26
            ->addArgument('parameters', InputArgument::IS_ARRAY, 'List of parameters (key:value)', [])
27
            ->addOption('tags', 't', InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, 'Add one or multiple (array) tags')
28
        ;
29
    }
30
31
    /**
32
     *
33
     * @param InputInterface $input
34
     * @param OutputInterface $output
35
     * @return int
36
     */
37 1
    protected function execute(InputInterface $input, OutputInterface $output): int
38
    {
39 1
        $jobName = $input->getArgument('job');
40
41 1
        $parameters = [];
42 1
        foreach ($input->getArgument('parameters') as $parameter) {
43 1
            list($name, $value) = explode(':', $parameter);
44 1
            $parameters[trim($name)] = trim($value);
45
        }
46
47 1
        $tags = [];
48 1
        foreach ($input->getOption('tags') as $tag) {
49 1
            $tags[] = trim($tag);
50
        }
51
52 1
        $task = new Task(
53 1
            new Profile($input->getArgument('profile')),
54 1
            new $jobName,
55 1
            $parameters, $tags
56
        );
57
58 1
        ServiceContainer::getInstance()
59 1
            ->queue
60 1
            ->add($task);
61
62 1
        $this->formatTaskBlock($task, $output);
63
64 1
        return 0;
65
    }
66
}
67