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\OutputInterface; |
18
|
|
|
use Task\Handler\TaskHandlerFactoryInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Run pending tasks. |
22
|
|
|
*/ |
23
|
|
|
class RunHandlerCommand extends Command |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var TaskHandlerFactoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $handlerFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $name |
32
|
|
|
* @param TaskHandlerFactoryInterface $handlerFactory |
33
|
|
|
*/ |
34
|
|
|
public function __construct($name, TaskHandlerFactoryInterface $handlerFactory) |
35
|
|
|
{ |
36
|
|
|
parent::__construct($name); |
37
|
|
|
|
38
|
|
|
$this->handlerFactory = $handlerFactory; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
protected function configure() |
45
|
|
|
{ |
46
|
|
|
$this |
47
|
|
|
->setDescription('Run handler') |
48
|
|
|
->setHelp(<<<'EOT' |
49
|
|
|
The <info>%command.name%</info> command run given handler. |
50
|
|
|
|
51
|
|
|
$ %command.full_name% AppBundle\\Handler\\ImageHandler ./img/test-image.jpg |
52
|
|
|
EOT |
53
|
|
|
) |
54
|
|
|
->addArgument('handlerClass', InputArgument::REQUIRED, 'Handler which will be called') |
55
|
|
|
->addArgument('workload', InputArgument::OPTIONAL, 'This will be passed to the handler'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
62
|
|
|
{ |
63
|
|
|
$handlerClass = $input->getArgument('handlerClass'); |
64
|
|
|
$workload = $input->getArgument('workload'); |
65
|
|
|
|
66
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
67
|
|
|
$output->writeln(sprintf('Run command "%s" with workload "%s"', $handlerClass, $workload)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$result = $this->handlerFactory->create($handlerClass)->handle($workload); |
|
|
|
|
71
|
|
|
|
72
|
|
|
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
73
|
|
|
$output->writeln(sprintf('Result: %s', json_encode($result))); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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.