-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStubTerminableCommand.php
68 lines (53 loc) · 1.88 KB
/
StubTerminableCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
declare(strict_types=1);
namespace Facile\TerminableLoop\Tests\Stub;
use Facile\TerminableLoop\AbstractTerminableCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
class StubTerminableCommand extends AbstractTerminableCommand
{
public function __construct()
{
parent::__construct('stub:terminable:sleep');
}
public function configure(): void
{
$this->addOption('stub', null, InputOption::VALUE_REQUIRED, 'Stubbed execution duration time', '0');
$this->addOption('sleep', null, InputOption::VALUE_REQUIRED, 'Sleep duration time', '0');
}
protected function commandBody(InputInterface $input, OutputInterface $output): int
{
$stubDuration = $this->getOption($input, 'stub');
$sleepDuration = $this->getOption($input, 'sleep');
if ($stubDuration) {
$process = new Process(['sleep', $stubDuration]);
$process->run();
}
$output->writeln('Stub execution terminated');
if ($sleepDuration > 0) {
$this->setSleepDuration($sleepDuration);
} else {
$output->writeln('No sleep');
}
return 1; // force exit from bash
}
/**
* @throws \InvalidArgumentException
*/
protected function getOption(InputInterface $input, string $name): int
{
$paramValue = $input->getOption($name);
if (is_numeric($paramValue)) {
$value = (int) $paramValue;
if ($value < 0) {
throw new \InvalidArgumentException('Not a positive integer value');
}
return $value;
}
throw new \InvalidArgumentException(
'Can\'t return an int from ' . print_r($paramValue, true)
);
}
}