Completed
Push — master ( 745336...aadd5d )
by Matthew
06:17
created

RunCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 53
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 42
cts 42
cp 1
rs 9.5797
c 0
b 0
f 0
cc 1
eloc 43
nc 1
nop 0
crap 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Dtc\QueueBundle\Command;
4
5
use Dtc\QueueBundle\Model\Job;
6
use Dtc\QueueBundle\Model\Run;
7
use Dtc\QueueBundle\Run\Loop;
8
use Dtc\QueueBundle\Util\Util;
9
use Psr\Log\LoggerInterface;
10
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
class RunCommand extends ContainerAwareCommand
17
{
18 1
    protected function configure()
19
    {
20
        $this
21 1
            ->setName('dtc:queue:run')
22 1
            ->setDefinition(
23
                array(
24 1
                    new InputArgument('worker_name', InputArgument::OPTIONAL, 'Name of worker', null),
25 1
                    new InputArgument('method', InputArgument::OPTIONAL, 'DI method of worker', null),
26 1
                    new InputOption(
27 1
                        'id',
28 1
                        'i',
29 1
                        InputOption::VALUE_REQUIRED,
30 1
                        'Id of Job to run',
31 1
                        null
32
                    ),
33 1
                    new InputOption(
34 1
                        'max_count',
35 1
                        'm',
36 1
                        InputOption::VALUE_REQUIRED,
37 1
                        'Maximum number of jobs to work on before exiting',
38 1
                        null
39
                    ),
40 1
                    new InputOption(
41 1
                        'duration',
42 1
                        'd',
43 1
                        InputOption::VALUE_REQUIRED,
44 1
                        'Duration to run for in seconds',
45 1
                        null
46
                    ),
47 1
                    new InputOption(
48 1
                        'timeout',
49 1
                        't',
50 1
                        InputOption::VALUE_REQUIRED,
51 1
                        'Process timeout in seconds (hard exit of process regardless)',
52 1
                        3600
53
                    ),
54 1
                    new InputOption(
55 1
                        'nano_sleep',
56 1
                        's',
57 1
                        InputOption::VALUE_REQUIRED,
58 1
                        'If using duration, this is the time to sleep when there\'s no jobs in nanoseconds',
59 1
                        500000000
60
                    ),
61 1
                    new InputOption(
62 1
                        'logger',
63 1
                        'l',
64 1
                        InputOption::VALUE_REQUIRED,
65 1
                        'Log using the logger service specified, or output to console if null (or an invalid logger service id) is passed in'
66
                    ),
67
                )
68
            )
69 1
            ->setDescription('Start up a job in queue');
70 1
    }
71
72 1
    protected function execute(InputInterface $input, OutputInterface $output)
73
    {
74 1
        $start = microtime(true);
75 1
        $this->output = $output;
0 ignored issues
show
Bug introduced by
The property output does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
76 1
        $container = $this->getContainer();
77 1
        $loop = $container->get('dtc_queue.run.loop');
78 1
        $loop->setOutput($output);
79 1
        $workerName = $input->getArgument('worker_name');
80 1
        $methodName = $input->getArgument('method');
81 1
        $maxCount = $input->getOption('max_count');
82 1
        $duration = $input->getOption('duration');
83 1
        $processTimeout = $input->getOption('timeout');
84 1
        $nanoSleep = $input->getOption('nano_sleep');
85 1
        $loggerService = $input->getOption('logger');
86
87 1
        $this->setLoggerService($loop, $loggerService);
88
89 1
        $maxCount = Util::validateIntNull('max_count', $maxCount, 32);
90 1
        $duration = Util::validateIntNull('duration', $duration, 32);
91 1
        $nanoSleep = Util::validateIntNull('nano_sleep', $nanoSleep, 63);
92 1
        $processTimeout = Util::validateIntNull('timeout', $processTimeout, 32);
93 1
        $loop->checkMaxCountDuration($maxCount, $duration, $processTimeout);
94
95
        // Check to see if there are other instances
96 1
        set_time_limit($processTimeout); // Set timeout on the process
97
98 1
        if ($jobId = $input->getOption('id')) {
99
            return $loop->runJobById($start, $jobId); // Run a single job
100
        }
101
102 1
        return $loop->runLoop($start, $workerName, $methodName, $maxCount, $duration, $nanoSleep);
103
    }
104
105 1
    protected function setLoggerService(Loop $loop, $loggerService)
106
    {
107 1
        if (!$loggerService) {
108 1
            return;
109
        }
110
111
        $container = $this->getContainer();
112
        if (!$container->has($loggerService)) {
113
            return;
114
        }
115
116
        $logger = $container->get($loggerService);
117
        if (!$logger instanceof LoggerInterface) {
118
            throw new \Exception("$loggerService must be instance of Psr\\Log\\LoggerInterface");
119
        }
120
        $loop->setLogger($logger);
121
    }
122
}
123