GearmanWorkerExecuteCommand::execute()   B
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 7.0012

Importance

Changes 0
Metric Value
dl 0
loc 64
ccs 33
cts 34
cp 0.9706
rs 7.8521
c 0
b 0
f 0
cc 7
nc 9
nop 2
crap 7.0012

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
/**
4
 * Gearman Bundle for Symfony2 / Symfony3
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * Feel free to edit as you please, and have fun.
10
 *
11
 * @author Marc Morera <[email protected]>
12
 * @author Mickael Perraud <[email protected]>
13
 */
14
15
namespace Mkk\GearmanBundle\Command;
16
17
use Symfony\Component\Console\Helper\QuestionHelper;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
23
use Symfony\Component\Console\Question\ConfirmationQuestion;
24
25
/**
26
 * Gearman Worker Execute Command class
27
 */
28
class GearmanWorkerExecuteCommand extends ContainerAwareCommand
29
{
30
    /**
31
     * Console Command configuration
32
     */
33 16
    protected function configure()
34
    {
35 16
        parent::configure();
36
37
        $this
38 16
            ->setName('gearman:worker:execute')
39 16
            ->setDescription('Execute one worker with all contained Jobs')
40 16
            ->addArgument(
41 16
                'worker',
42 16
                InputArgument::REQUIRED,
43 16
                'work to execute'
44
            )
45 16
            ->addOption(
46 16
                'no-description',
47 16
                null,
48 16
                InputOption::VALUE_NONE,
49 16
                'Don\'t print worker description'
50
            )
51 16
            ->addOption(
52 16
                'iterations',
53 16
                null,
54 16
                InputOption::VALUE_OPTIONAL,
55 16
                'Override configured iterations'
56
            )
57 16
            ->addOption(
58 16
                'minimum-execution-time',
59 16
                null,
60 16
                InputOption::VALUE_OPTIONAL,
61 16
                'Override configured minimum execution time'
62
            )
63 16
            ->addOption(
64 16
                'timeout',
65 16
                null,
66 16
                InputOption::VALUE_OPTIONAL,
67 16
                'Override configured timeout'
68
            );
69 16
    }
70
71
    /**
72
     * Executes the current command.
73
     *
74
     * @param InputInterface  $input  An InputInterface instance
75
     * @param OutputInterface $output An OutputInterface instance
76
     *
77
     * @return integer 0 if everything went fine, or an error code
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
78
     *
79
     * @throws \LogicException When this abstract class is not implemented
80
     */
81 3
    protected function execute(InputInterface $input, OutputInterface $output)
82
    {
83
        /**
84
         * @var QuestionHelper $question
85
         */
86
        $question = $this
87 3
            ->getHelperSet()
88 3
            ->get('question');
89
90
        if (
91 3
            !$input->getOption('no-interaction') &&
92 2
            !$question->ask(
93 2
                $input,
94 2
                $output,
95 3
                new ConfirmationQuestion('This will execute asked worker with all its jobs?')
96
            )
97
        ) {
98
            return;
99
        }
100
101 3
        if (!$input->getOption('quiet')) {
102
103 2
            $output->writeln(sprintf(
104 2
                '<info>[%s] loading...</info>',
105 2
                date('Y-m-d H:i:s')
106
            ));
107
        }
108
109 3
        $worker = $input->getArgument('worker');
110
111
        $workerStructure = $this
112 3
            ->getContainer()->get('gearman')
113 3
            ->getWorker($worker);
114
115
        if (
116 3
            !$input->getOption('no-description') &&
117 3
            !$input->getOption('quiet')
118
        ) {
119
            $this
120 2
                ->getContainer()->get('gearman.describer')
121 2
                ->describeWorker(
122 2
                    $output,
123 2
                    $workerStructure,
124 2
                    true
125
                );
126
        }
127
128 3
        if (!$input->getOption('quiet')) {
129
130 2
            $output->writeln(sprintf(
131 2
                '<info>[%s] loaded. Ctrl+C to break</info>',
132 2
                date('Y-m-d H:i:s')
133
            ));
134
        }
135
136
        $this
137 3
            ->getContainer()->get('gearman.execute')
138 3
            ->setOutput($output)
139 3
            ->executeWorker($worker, array(
140 3
                'iterations'             => $input->getOption('iterations'),
141 3
                'minimum_execution_time' => $input->getOption('minimum-execution-time'),
142 3
                'timeout'                => $input->getOption('timeout')
143
            ));
144 3
    }
145
}
146