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\Handler\TaskHandlerFactoryInterface; |
20
|
|
|
use Task\Storage\TaskExecutionRepositoryInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Executes given execution identified by uuid. |
24
|
|
|
*/ |
25
|
|
|
class ExecuteCommand extends Command |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var TaskHandlerFactoryInterface |
29
|
|
|
*/ |
30
|
|
|
private $handlerFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var TaskExecutionRepositoryInterface |
34
|
|
|
*/ |
35
|
|
|
private $executionRepository; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $name |
39
|
|
|
* @param TaskHandlerFactoryInterface $handlerFactory |
40
|
|
|
* @param TaskExecutionRepositoryInterface $executionRepository |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
$name, |
44
|
|
|
TaskHandlerFactoryInterface $handlerFactory, |
45
|
|
|
TaskExecutionRepositoryInterface $executionRepository |
46
|
|
|
) { |
47
|
|
|
parent::__construct($name); |
48
|
|
|
|
49
|
|
|
$this->handlerFactory = $handlerFactory; |
50
|
|
|
$this->executionRepository = $executionRepository; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
protected function configure() |
57
|
|
|
{ |
58
|
|
|
$this->addArgument('uuid', InputArgument::REQUIRED); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
65
|
|
|
{ |
66
|
|
|
$errOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output; |
67
|
|
|
|
68
|
|
|
$execution = $this->executionRepository->findByUuid($input->getArgument('uuid')); |
69
|
|
|
$handler = $this->handlerFactory->create($execution->getHandlerClass()); |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
$result = $handler->handle($execution->getWorkload()); |
73
|
|
|
} catch (\Exception $e) { |
74
|
|
|
$errOutput->writeln($e->__toString()); |
75
|
|
|
|
76
|
|
|
// Process exit-code: 0 = OK, >1 = FAIL |
77
|
|
|
return 1; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$output->write($result); |
|
|
|
|
81
|
|
|
|
82
|
|
|
return 0; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function isHidden() |
89
|
|
|
{ |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.