1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Clearcode\CommandBusConsole\Bundle\Command; |
4
|
|
|
|
5
|
|
|
use Matthias\SymfonyConsoleForm\Console\Helper\FormHelper; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use tests\Clearcode\CommandBusConsole\Bundle\Type\DummyCommandType; |
11
|
|
|
|
12
|
|
|
class CommandBusInteractiveConsoleCommand extends ContainerAwareCommand |
13
|
|
|
{ |
14
|
|
|
const SUCCESS_CODE = 0; |
15
|
|
|
const ERROR_CODE = 1; |
16
|
|
|
|
17
|
|
|
const INTERACTIVE_COMMAND = 'command-bus:handle'; |
18
|
|
|
|
19
|
|
|
private static $commandToFormTypeMap = [ |
|
|
|
|
20
|
|
|
'DummyCommand' => DummyCommandType::class, |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
protected function configure() |
24
|
|
|
{ |
25
|
|
|
$this |
26
|
|
|
->setName(self::INTERACTIVE_COMMAND) |
27
|
|
|
->setDescription('Interactive CLI for command bus.') |
28
|
|
|
->addArgument('commandName', InputArgument::REQUIRED); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** {@inheritdoc} */ |
32
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
33
|
|
|
{ |
34
|
|
|
/** @var FormHelper $formHelper */ |
35
|
|
|
$formHelper = $this->getHelper('form'); |
36
|
|
|
|
37
|
|
|
$commandName = $input->getArgument('commandName'); |
38
|
|
|
$command = $formHelper->interactUsingForm(new self::$commandToFormTypeMap[$commandName](), $input, $output); |
39
|
|
|
|
40
|
|
|
try { |
41
|
|
|
$this->getContainer()->get('command_bus')->handle($command); |
42
|
|
|
} catch (\Exception $e) { |
43
|
|
|
return $this->handleException($output, $e); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->handleSuccess($output, $commandName); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function handleException(OutputInterface $output, \Exception $exception) |
50
|
|
|
{ |
51
|
|
|
$output->writeln(sprintf('<error>%s</error>', $exception->getMessage())); |
52
|
|
|
|
53
|
|
|
return self::ERROR_CODE; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function handleSuccess(OutputInterface $output, $commandName) |
57
|
|
|
{ |
58
|
|
|
$output->writeln(sprintf('The <info>%s</info> executed with success.', $commandName)); |
59
|
|
|
|
60
|
|
|
return self::SUCCESS_CODE; |
61
|
|
|
} |
62
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.