Completed
Push — master ( b7e642...0ba8e7 )
by Paweł
04:39 queued 02:16
created

CommandBusHandleCommand::parseNamedArguments()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4286
cc 3
eloc 7
nc 3
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A CommandBusHandleCommand::handleSuccess() 0 6 1
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