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

GearmanJobExecuteCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 28
cts 28
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 28
nc 1
nop 0
crap 1
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 Job Execute Command class
29
 */
30
class GearmanJobExecuteCommand 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
     * @var QuestionHelper
55
     *
56
     * Question
57
     */
58
    protected $question;
59
60
    /**
61
     * Set gearman client
62
     *
63
     * @param GearmanClient $gearmanClient Gearman client
64
     *
65
     * @return GearmanJobExecuteCommand self Object
66
     */
67 8
    public function setGearmanClient(GearmanClient $gearmanClient)
68
    {
69 8
        $this->gearmanClient = $gearmanClient;
70
71 8
        return $this;
72
    }
73
74
    /**
75
     * set Gearman describer
76
     *
77
     * @param GearmanDescriber $gearmanDescriber GearmanDescriber
78
     *
79
     * @return GearmanJobExecuteCommand self Object
80
     */
81 8
    public function setGearmanDescriber(GearmanDescriber $gearmanDescriber)
82
    {
83 8
        $this->gearmanDescriber = $gearmanDescriber;
84
85 8
        return $this;
86
    }
87
88
    /**
89
     * set Gearman execute
90
     *
91
     * @param GearmanExecute $gearmanExecute GearmanExecute
92
     *
93
     * @return GearmanJobExecuteCommand self Object
94
     */
95 8
    public function setGearmanExecute(GearmanExecute $gearmanExecute)
96
    {
97 8
        $this->gearmanExecute = $gearmanExecute;
98
99 8
        return $this;
100
    }
101
102
    /**
103
     * Console Command configuration
104
     */
105 8
    protected function configure()
106
    {
107
        $this
108 8
            ->setName('gearman:job:execute')
109 8
            ->setDescription('Execute one single job')
110 8
            ->addArgument(
111 8
                'job',
112 8
                InputArgument::REQUIRED,
113 8
                'job to execute'
114
            )
115 8
            ->addOption(
116 8
                'no-description',
117 8
                null,
118 8
                InputOption::VALUE_NONE,
119 8
                'Don\'t print job description'
120
            )
121 8
            ->addOption(
122 8
                'iterations',
123 8
                null,
124 8
                InputOption::VALUE_OPTIONAL,
125 8
                'Override configured iterations'
126
            )
127 8
            ->addOption(
128 8
                'minimum-execution-time',
129 8
                null,
130 8
                InputOption::VALUE_OPTIONAL,
131 8
                'Override configured minimum execution time'
132
            )
133 8
            ->addOption(
134 8
                'timeout',
135 8
                null,
136 8
                InputOption::VALUE_OPTIONAL,
137 8
                'Override configured timeout'
138
            );
139 8
    }
140
141
    /**
142
     * Executes the current command.
143
     *
144
     * @param InputInterface  $input  An InputInterface instance
145
     * @param OutputInterface $output An OutputInterface instance
146
     *
147
     * @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...
148
     *
149
     * @throws \LogicException When this abstract class is not implemented
150
     */
151 8
    protected function execute(InputInterface $input, OutputInterface $output)
152
    {
153
        /**
154
         * @var QuestionHelper $question
155
         */
156 8
        $question = $this->getHelperSet()->get('question');
157
158
        if (
159 8
            !$input->getOption('no-interaction') &&
160 4
            !$question->ask(
161
                $input,
162
                $output,
163 8
                new ConfirmationQuestion('This will execute asked job?')
164
            )
165
        ) {
166 2
            return;
167
        }
168
169 6
        if (!$input->getOption('quiet')) {
170
171 3
            $output->writeln(sprintf(
172 3
                '<info>[%s] loading...</info>',
173 3
                date('Y-m-d H:i:s')
174
            ));
175
        }
176
177 6
        $job = $input->getArgument('job');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
178
        $jobStructure = $this
179 6
            ->gearmanClient
180 6
            ->getJob($job);
181
182
        if (
183 6
            !$input->getOption('no-description') &&
184 6
            !$input->getOption('quiet')
185
        ) {
186
187
            $this
188 3
                ->gearmanDescriber
189 3
                ->describeJob(
190
                    $output,
191
                    $jobStructure
192
                );
193
        }
194
195 6
        if (!$input->getOption('quiet')) {
196
197 3
            $output->writeln(sprintf(
198 3
                '<info>[%s] loaded. Ctrl+C to break</info>',
199 3
                date('Y-m-d H:i:s')
200
            ));
201
        }
202
203
        $this
204 6
            ->gearmanExecute
205 6
            ->setOutput($output)
206 6
            ->executeJob($job, array(
207 6
                'iterations'             => $input->getOption('iterations'),
208 6
                'minimum_execution_time' => $input->getOption('minimum-execution-time'),
209 6
                'timeout'                => $input->getOption('timeout')
210
            ));
211 6
    }
212
}
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...
213