|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of php-task library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) php-task |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Task\TaskBundle\Command; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Command\Command; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Output\ConsoleOutputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
use Task\Executor\FailedException; |
|
20
|
|
|
use Task\Handler\TaskHandlerFactoryInterface; |
|
21
|
|
|
use Task\Storage\TaskExecutionRepositoryInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Executes given execution identified by uuid. |
|
25
|
|
|
*/ |
|
26
|
|
|
class ExecuteCommand extends Command |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var TaskHandlerFactoryInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $handlerFactory; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var TaskExecutionRepositoryInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $executionRepository; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $name |
|
40
|
|
|
* @param TaskHandlerFactoryInterface $handlerFactory |
|
41
|
|
|
* @param TaskExecutionRepositoryInterface $executionRepository |
|
42
|
|
|
*/ |
|
43
|
13 |
|
public function __construct( |
|
44
|
|
|
$name, |
|
45
|
|
|
TaskHandlerFactoryInterface $handlerFactory, |
|
46
|
|
|
TaskExecutionRepositoryInterface $executionRepository |
|
47
|
|
|
) { |
|
48
|
13 |
|
parent::__construct($name); |
|
49
|
|
|
|
|
50
|
13 |
|
$this->handlerFactory = $handlerFactory; |
|
51
|
13 |
|
$this->executionRepository = $executionRepository; |
|
52
|
13 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
13 |
|
protected function configure() |
|
58
|
|
|
{ |
|
59
|
13 |
|
$this->addArgument('uuid', InputArgument::REQUIRED); |
|
60
|
13 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
66
|
|
|
{ |
|
67
|
|
|
$errorOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output; |
|
68
|
|
|
|
|
69
|
|
|
$execution = $this->executionRepository->findByUuid($input->getArgument('uuid')); |
|
70
|
|
|
$handler = $this->handlerFactory->create($execution->getHandlerClass()); |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
|
|
$result = $handler->handle($execution->getWorkload()); |
|
74
|
|
|
} catch (\Exception $exception) { |
|
75
|
|
|
if ($exception instanceof FailedException) { |
|
76
|
|
|
$errorOutput->writeln(FailedException::class); |
|
77
|
|
|
$exception = $exception->getPrevious(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$errorOutput->writeln($exception->__toString()); |
|
81
|
|
|
|
|
82
|
|
|
// Process exit-code: 0 = OK, >1 = FAIL |
|
83
|
|
|
return 1; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$output->write($result); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
return 0; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function isHidden() |
|
95
|
|
|
{ |
|
96
|
|
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.