Completed
Pull Request — master (#11)
by
unknown
05:47
created

CommandBusInteractiveConsoleCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
c 2
b 0
f 1
lcom 1
cbo 6
dl 0
loc 51
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A execute() 0 16 2
A handleException() 0 6 1
A handleSuccess() 0 6 1
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 = [
0 ignored issues
show
Unused Code introduced by
The property $commandToFormTypeMap is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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
}