Completed
Push — master ( 0726a8...336a87 )
by Emil
02:19
created

RunCommand::runId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
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
            $this->runId($input, $runner);
39
        } else {
40
            $response = $runner->runTask($input->getArgument('service'));
41
42
            if (!$input->getOption('silent')) {
43
                $output->writeln($response);
44
            }
45
        }
46
    }
47
48
    /**
49
     * @param InputInterface $input
50
     * @param TaskRunner $runner
51
     * @throws NoResultException
52
     */
53
    protected function runId(InputInterface $input, TaskRunner $runner)
54
    {
55
        $task = $this->getContainer()
56
            ->get('doctrine')
57
            ->getManager()
58
            ->getRepository('GloobyTaskBundle:QueuedTask')
59
            ->find($input->getOption('id'));
60
61
        if (null === $task) {
62
            throw new NoResultException();
63
        }
64
65
        $runner->run($task);
66
    }
67
}
68