Passed
Push — v2 ( c4de3c...f057b3 )
by Brice
03:43
created

AddTask   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 69.23%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 50
ccs 18
cts 26
cp 0.6923
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
B execute() 0 28 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 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