Completed
Push — master ( 7a08de...9ce2ea )
by Daniel
04:39
created

NewCommand::initialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 6
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('task:new')
31
            ->setDescription('Create a 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 ($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