Completed
Push — master ( 088f76...02d8b1 )
by Emil
03:59
created

RunCommand::execute()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 14
nc 5
nop 2
1
<?php
2
3
namespace Glooby\TaskBundle\Command\Task;
4
5
use Doctrine\ORM\NoResultException;
6
use Glooby\TaskBundle\Task\TaskRunner;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * @author Emil Kilhage
15
 */
16
class RunCommand extends ContainerAwareCommand
17
{
18
    /**
19
     * Configures the current command.
20
     */
21
    protected function configure()
22
    {
23
        $this->setName('task:run');
24
        $this->addArgument('service', InputArgument::OPTIONAL);
25
        $this->addOption('silent', 'S', InputOption::VALUE_NONE);
26
        $this->addOption('id', null, InputOption::VALUE_REQUIRED);
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        $runner = $this->getContainer()->get('glooby_task.task_runner');
35
        $runner->setOutput($output);
36
37
        if ($input->getOption('id')) {
38
            $response = $this->runId($input, $runner);
39
40
            if (!$input->getOption('silent')) {
41
                if (!empty($response)) {
42
                    $output->writeln("task {$input->getOption('id')} finished: $response");
43
                } else {
44
                    $output->writeln("task {$input->getOption('id')} finished");
45
                }
46
            }
47
        } else {
48
            $response = $runner->runTask($input->getArgument('service'));
49
50
            if (!$input->getOption('silent')) {
51
                $output->writeln($response);
52
            }
53
        }
54
    }
55
56
    /**
57
     * @param InputInterface $input
58
     * @param TaskRunner $runner
59
     * @throws NoResultException
60
     */
61
    protected function runId(InputInterface $input, TaskRunner $runner)
62
    {
63
        $task = $this->getContainer()
64
            ->get('doctrine')
65
            ->getManager()
66
            ->getRepository('GloobyTaskBundle:QueuedTask')
67
            ->find($input->getOption('id'));
68
69
        if (null === $task) {
70
            throw new NoResultException();
71
        }
72
73
        return $runner->run($task);
74
    }
75
}
76