Completed
Push — master ( 272693...b768a3 )
by Daniel
04:04
created

NewCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 19
ccs 0
cts 15
cp 0
rs 9.4285
cc 1
eloc 14
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of the Commander project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Commander\Console\Command;
9
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
/**
16
 * New command class.
17
 *
18
 * @package GravityMedia\Commander\Console\Command
19
 */
20
class NewCommand extends Command
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function configure()
26
    {
27
        parent::configure();
28
29
        $this
30
            ->setName('new')
31
            ->setDescription('Create new task')
32
            ->addArgument(
33
                'commandline',
34
                InputArgument::REQUIRED,
35
                'The commandline to run with the task'
36
            )
37
            ->addOption(
38
                'priority',
39
                null,
40
                InputOption::VALUE_REQUIRED,
41
                'The task priority'
42
            );
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $taskManager = $this->getCommander()->getTaskManager();
51
52
        $commandline = $input->getArgument('commandline');
53
        $task = $taskManager->findNextTask(['commandline' => $commandline]);
54
55
        $priority = filter_var($input->getOption('priority'), FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
56
57
        if (null === $task) {
58
            $task = $taskManager->newTask($commandline, $priority);
59
            $output->writeln(sprintf('Created new task %s', $task->getEntity()->getId()));
60
            return;
61
        }
62
63
        if (null !== $priority && $priority !== $task->getEntity()->getPriority()) {
64
            $task->prioritize($priority);
65
            $output->writeln(sprintf('Updated task priority of %s', $task->getEntity()->getId()));
66
            return;
67
        }
68
69
        $output->writeln(sprintf('Task %s already present, ignoring', $task->getEntity()->getId()));
70
    }
71
}
72