Completed
Push — master ( 81a590...b3f90f )
by Daniel
07:15 queued 01:42
created

JoinCommand::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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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
 * Join command class.
17
 *
18
 * @package GravityMedia\Commander\Console\Command
19
 */
20
class JoinCommand extends Command
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function configure()
26
    {
27
        parent::configure();
28
29
        $this
30
            ->setName('join')
31
            ->setDescription('Create new task or update existing 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
        $commander = $this->getCommander()->initialize();
51
        $taskManager = $commander->getTaskManager();
52
53
        $commandline = $input->getArgument('commandline');
54
        $task = $taskManager->findNextTask(['commandline' => $commandline]);
55
56
        $priority = filter_var($input->getOption('priority'), FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
57
58
        if (null === $task) {
59
            $task = $taskManager->newTask($commandline, $priority);
60
            $output->writeln(sprintf('Created new task %s', $task->getEntity()->getId()));
61
            return;
62
        }
63
64
        if (null !== $priority && $priority !== $task->getEntity()->getPriority()) {
65
            $task->prioritize($priority);
66
            $output->writeln(sprintf('Updated the priority of task %s', $task->getEntity()->getId()));
67
            return;
68
        }
69
70
        $output->writeln(sprintf('Task %s already present, ignoring', $task->getEntity()->getId()));
71
    }
72
}
73