CommandBusHandleCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 5

Importance

Changes 8
Bugs 2 Features 2
Metric Value
wmc 9
c 8
b 2
f 2
lcom 3
cbo 5
dl 0
loc 85
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A formType() 0 4 1
A configure() 0 6 1
A execute() 0 10 2
A handleException() 0 12 1
A handleSuccess() 0 6 1
A getArgumentsString() 0 10 2
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
use Carbon\Carbon;
10
11
class CommandBusHandleCommand extends InteractiveFormContainerAwareCommand
12
{
13
    const SUCCESS_CODE = 0;
14
    const ERROR_CODE = 1;
15
16
    /** @var string */
17
    private $alias;
18
19
    /** @var string */
20
    private $formType;
21
22
    /** @var string */
23
    private $legacyFormTypeAlias;
24
25
    /**
26
     * @param string $alias
27
     * @param string $formType
28
     * @param string $legacyFormTypeAlias
29
     */
30
    public function __construct($alias, $formType, $legacyFormTypeAlias)
31
    {
32
        $this->alias = $alias;
33
        $this->formType = $formType;
34
        $this->legacyFormTypeAlias = $legacyFormTypeAlias;
35
36
        parent::__construct();
37
    }
38
39
    /** {@inheritdoc} */
40
    public function formType()
41
    {
42
        return LegacyFormHelper::getType($this->formType, $this->legacyFormTypeAlias);
43
    }
44
45
    /** {@inheritdoc} */
46
    protected function configure()
47
    {
48
        $this
49
            ->setName(sprintf('command-bus:%s', $this->alias))
50
        ;
51
    }
52
53
    /** {@inheritdoc} */
54
    protected function execute(InputInterface $input, OutputInterface $output)
55
    {
56
        try {
57
            $this->getContainer()->get('command_bus')->handle($this->formData());
58
        } catch (\Exception $e) {
59
            return $this->handleException($output, $e);
60
        }
61
62
        return $this->handleSuccess($output, get_class($this->formData()));
63
    }
64
65
    private function handleException(OutputInterface $output, \Exception $exception)
66
    {
67
        $command = $this->formData();
68
        $arguments = $this->getArgumentsString($command);
69
70
        $output->writeln(sprintf(
71
            '<error>[%s] The command "%s" with arguments [%s] has failed to execute. Exception "%s" was thrown with message: "%s"</error>',
72
            Carbon::now(), get_class($command), $arguments, get_class($exception), $exception->getMessage()
73
        ));
74
75
        return self::ERROR_CODE;
76
    }
77
78
    private function handleSuccess(OutputInterface $output, $commandToLunch)
79
    {
80
        $output->writeln(sprintf('[%s] The <info>%s</info> executed with success.', Carbon::now(), $commandToLunch));
81
82
        return self::SUCCESS_CODE;
83
    }
84
85
    private function getArgumentsString($command)
86
    {
87
        $argumentStrings = [];
88
89
        foreach (get_object_vars($command) as $propertyName => $propertyValue) {
90
            $argumentStrings[] = sprintf('%s=>"%s"', $propertyName, $propertyValue);
91
        }
92
93
        return implode(', ', $argumentStrings);
94
    }
95
}
96