Completed
Push — master ( 355f41...200862 )
by Mickael
06:25
created

GearmanWorkerExecuteCommand::execute()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 64
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 64
ccs 30
cts 30
cp 1
rs 7.2058
c 0
b 0
f 0
cc 7
eloc 39
nc 9
nop 2
crap 7

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
 */
13
14
namespace Mkk\GearmanBundle\Command;
15
16
use Symfony\Component\Console\Helper\QuestionHelper;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Mkk\GearmanBundle\Command\Abstracts\AbstractGearmanCommand;
22
use Mkk\GearmanBundle\Service\GearmanClient;
23
use Mkk\GearmanBundle\Service\GearmanDescriber;
24
use Mkk\GearmanBundle\Service\GearmanExecute;
25
use Symfony\Component\Console\Question\ConfirmationQuestion;
26
27
/**
28
 * Gearman Worker Execute Command class
29
 */
30
class GearmanWorkerExecuteCommand extends AbstractGearmanCommand
31
{
32
    /**
33
     * @var GearmanClient
34
     *
35
     * Gearman client
36
     */
37
    protected $gearmanClient;
38
39
    /**
40
     * @var GearmanDescriber
41
     *
42
     * GearmanDescriber
43
     */
44
    protected $gearmanDescriber;
45
46
    /**
47
     * @var GearmanExecute
48
     *
49
     * Gearman execute
50
     */
51
    protected $gearmanExecute;
52
53
    /**
54
     * Set gearman client
55
     *
56
     * @param GearmanClient $gearmanClient Gearman client
57
     *
58
     * @return GearmanWorkerExecuteCommand self Object
59
     */
60 8
    public function setGearmanClient(GearmanClient $gearmanClient)
61
    {
62 8
        $this->gearmanClient = $gearmanClient;
63
64 8
        return $this;
65
    }
66
67
    /**
68
     * set Gearman describer
69
     *
70
     * @param GearmanDescriber $gearmanDescriber GearmanDescriber
71
     *
72
     * @return GearmanWorkerExecuteCommand self Object
73
     */
74 8
    public function setGearmanDescriber(GearmanDescriber $gearmanDescriber)
75
    {
76 8
        $this->gearmanDescriber = $gearmanDescriber;
77
78 8
        return $this;
79
    }
80
81
    /**
82
     * set Gearman execute
83
     *
84
     * @param GearmanExecute $gearmanExecute GearmanExecute
85
     *
86
     * @return GearmanWorkerExecuteCommand self Object
87
     */
88 8
    public function setGearmanExecute(GearmanExecute $gearmanExecute)
89
    {
90 8
        $this->gearmanExecute = $gearmanExecute;
91
92 8
        return $this;
93
    }
94
95
    /**
96
     * Console Command configuration
97
     */
98 8
    protected function configure()
99
    {
100 8
        parent::configure();
101
102
        $this
103 8
            ->setName('gearman:worker:execute')
104 8
            ->setDescription('Execute one worker with all contained Jobs')
105 8
            ->addArgument(
106 8
                'worker',
107 8
                InputArgument::REQUIRED,
108 8
                'work to execute'
109
            )
110 8
            ->addOption(
111 8
                'no-description',
112 8
                null,
113 8
                InputOption::VALUE_NONE,
114 8
                'Don\'t print worker description'
115
            )
116 8
            ->addOption(
117 8
                'iterations',
118 8
                null,
119 8
                InputOption::VALUE_OPTIONAL,
120 8
                'Override configured iterations'
121
            )
122 8
            ->addOption(
123 8
                'minimum-execution-time',
124 8
                null,
125 8
                InputOption::VALUE_OPTIONAL,
126 8
                'Override configured minimum execution time'
127
            )
128 8
            ->addOption(
129 8
                'timeout',
130 8
                null,
131 8
                InputOption::VALUE_OPTIONAL,
132 8
                'Override configured timeout'
133
            );
134 8
    }
135
136
    /**
137
     * Executes the current command.
138
     *
139
     * @param InputInterface  $input  An InputInterface instance
140
     * @param OutputInterface $output An OutputInterface instance
141
     *
142
     * @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...
143
     *
144
     * @throws \LogicException When this abstract class is not implemented
145
     */
146 8
    protected function execute(InputInterface $input, OutputInterface $output)
147
    {
148
        /**
149
         * @var QuestionHelper $question
150
         */
151
        $question = $this
152 8
            ->getHelperSet()
153 8
            ->get('question');
154
155
        if (
156 8
            !$input->getOption('no-interaction') &&
157 4
            !$question->ask(
158
                $input,
159
                $output,
160 8
                new ConfirmationQuestion('This will execute asked worker with all its jobs?')
161
            )
162
        ) {
163 2
            return;
164
        }
165
166 6
        if (!$input->getOption('quiet')) {
167
168 3
            $output->writeln(sprintf(
169 3
                '<info>[%s] loading...</info>',
170 3
                date('Y-m-d H:i:s')
171
            ));
172
        }
173
174 6
        $worker = $input->getArgument('worker');
175
176
        $workerStructure = $this
177 6
            ->gearmanClient
178 6
            ->getWorker($worker);
179
180
        if (
181 6
            !$input->getOption('no-description') &&
182 6
            !$input->getOption('quiet')
183
        ) {
184
            $this
185 3
                ->gearmanDescriber
186 3
                ->describeWorker(
187
                    $output,
188
                    $workerStructure,
189 3
                    true
190
                );
191
        }
192
193 6
        if (!$input->getOption('quiet')) {
194
195 3
            $output->writeln(sprintf(
196 3
                '<info>[%s] loaded. Ctrl+C to break</info>',
197 3
                date('Y-m-d H:i:s')
198
            ));
199
        }
200
201
        $this
202 6
            ->gearmanExecute
203 6
            ->setOutput($output)
204 6
            ->executeWorker($worker, array(
205 6
                'iterations'             => $input->getOption('iterations'),
206 6
                'minimum_execution_time' => $input->getOption('minimum-execution-time'),
207 6
                'timeout'                => $input->getOption('timeout')
208
            ));
209 6
    }
210
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
211