1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Clearcode\CommandBusConsole\Bundle\Command; |
4
|
|
|
|
5
|
|
|
use Clearcode\CommandBusConsole\Bundle\LegacyFormHelper; |
6
|
|
|
use Matthias\SymfonyConsoleForm\Console\Command\InteractiveFormContainerAwareCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
class CommandBusHandleCommand extends InteractiveFormContainerAwareCommand |
11
|
|
|
{ |
12
|
|
|
const SUCCESS_CODE = 0; |
13
|
|
|
const ERROR_CODE = 1; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
private $alias; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $formType; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
private $legacyFormTypeAlias; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $alias |
26
|
|
|
* @param string $formType |
27
|
|
|
* @param string $legacyFormTypeAlias |
28
|
|
|
*/ |
29
|
|
|
public function __construct($alias, $formType, $legacyFormTypeAlias) |
30
|
|
|
{ |
31
|
|
|
$this->alias = $alias; |
32
|
|
|
$this->formType = $formType; |
33
|
|
|
$this->legacyFormTypeAlias = $legacyFormTypeAlias; |
34
|
|
|
|
35
|
|
|
parent::__construct(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** {@inheritdoc} */ |
39
|
|
|
public function formType() |
40
|
|
|
{ |
41
|
|
|
return LegacyFormHelper::getType($this->formType, $this->legacyFormTypeAlias); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** {@inheritdoc} */ |
45
|
|
|
protected function configure() |
46
|
|
|
{ |
47
|
|
|
$this |
48
|
|
|
->setName(sprintf('command-bus:%s', $this->alias)) |
49
|
|
|
; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** {@inheritdoc} */ |
53
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
|
|
$this->getContainer()->get('command_bus')->handle($this->formData()); |
57
|
|
|
} catch (\Exception $e) { |
58
|
|
|
return $this->handleException($output, $e); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->handleSuccess($output, get_class($this->formData())); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function handleException(OutputInterface $output, \Exception $exception) |
65
|
|
|
{ |
66
|
|
|
$output->writeln(sprintf('<error>%s</error>', $exception->getMessage())); |
67
|
|
|
|
68
|
|
|
return self::ERROR_CODE; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function handleSuccess(OutputInterface $output, $commandToLunch) |
72
|
|
|
{ |
73
|
|
|
$output->writeln(sprintf('The <info>%s</info> executed with success.', $commandToLunch)); |
74
|
|
|
|
75
|
|
|
return self::SUCCESS_CODE; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|