RunCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 7 1
A execute() 0 23 5
A runId() 0 14 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\Component\Console\Command\Command;
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
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
/**
15
 * @author Emil Kilhage
16
 */
17
class RunCommand extends Command
18
{
19
    protected static $defaultName = 'task:run';
20
    private $container;
21
22
    public function __construct(ContainerInterface $container){
23
        parent::__construct();
24
        $this->container = $container;
25
    }
26
27
    /**
28
     * Configures the current command.
29
     */
30
    protected function configure()
31
    {
32
        $this->setName('task:run');
33
        $this->addArgument('service', InputArgument::OPTIONAL);
34
        $this->addOption('silent', 'S', InputOption::VALUE_NONE);
35
        $this->addOption('id', null, InputOption::VALUE_REQUIRED);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $runner =  $this->container->get('glooby_task.task_runner');
44
        $runner->setOutput($output);
45
46
        if ($input->getOption('id')) {
47
            $response = $this->runId($input, $runner);
0 ignored issues
show
Documentation introduced by
$runner is of type object|null, but the function expects a object<Glooby\TaskBundle\Task\TaskRunner>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
49
            if (!$input->getOption('silent')) {
50
                if (!empty($response)) {
51
                    $output->writeln("task {$input->getOption('id')} finished: $response");
52
                } else {
53
                    $output->writeln("task {$input->getOption('id')} finished");
54
                }
55
            }
56
        } else {
57
            $response = $runner->runTask($input->getArgument('service'));
58
59
            if (!$input->getOption('silent')) {
60
                $output->writeln($response);
61
            }
62
        }
63
    }
64
65
    /**
66
     * @param InputInterface $input
67
     * @param TaskRunner $runner
68
     * @throws NoResultException
69
     */
70
    protected function runId(InputInterface $input, TaskRunner $runner)
71
    {
72
        $task = $this->container
73
            ->get('doctrine')
74
            ->getManager()
75
            ->getRepository('GloobyTaskBundle:QueuedTask')
76
            ->find($input->getOption('id'));
77
78
        if (null === $task) {
79
            throw new NoResultException();
80
        }
81
82
        return $runner->run($task);
83
    }
84
}
85