Passed
Push — master ( 519720...c45b74 )
by Hannes
02:29
created

SymfonyCommandAdapter::execute()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 53
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 31
nc 14
nop 2
dl 0
loc 53
rs 8.8017
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of byrokrat\giroapp.
4
 *
5
 * byrokrat\giroapp is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * byrokrat\giroapp is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016-19 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\giroapp\Console;
24
25
use byrokrat\giroapp\CommandBus\Commit;
26
use byrokrat\giroapp\CommandBus\Rollback;
27
use byrokrat\giroapp\Event\ExecutionStarted;
28
use byrokrat\giroapp\Event\ExecutionStopped;
29
use byrokrat\giroapp\Event\LogEvent;
30
use byrokrat\giroapp\Exception as GiroappException;
31
use byrokrat\giroapp\Event\Listener\OutputtingListener;
32
use byrokrat\giroapp\Plugin\EnvironmentInterface;
33
use Psr\Log\LogLevel;
34
use Symfony\Component\Console\Command\Command;
35
use Symfony\Component\Console\Input\InputInterface;
36
use Symfony\Component\Console\Output\ConsoleOutputInterface;
37
use Symfony\Component\Console\Output\OutputInterface;
38
use Psr\EventDispatcher\EventDispatcherInterface;
39
40
final class SymfonyCommandAdapter extends Command
41
{
42
    /** @var ConsoleInterface */
43
    private $console;
44
45
    /** @var EnvironmentInterface */
46
    private $environment;
47
48
    /** @var EventDispatcherInterface */
49
    private $dispatcher;
50
51
    public function __construct(
52
        ConsoleInterface $console,
53
        EnvironmentInterface $environment,
54
        EventDispatcherInterface $dispatcher
55
    ) {
56
        $this->console = $console;
57
        $this->environment = $environment;
58
        $this->dispatcher = $dispatcher;
59
        parent::__construct();
60
    }
61
62
    protected function configure(): void
63
    {
64
        $this->console->configure($this);
65
    }
66
67
    protected function execute(InputInterface $input, OutputInterface $output): int
68
    {
69
        if (!$output instanceof ConsoleOutputInterface) {
70
            throw new \InvalidArgumentException('Output must implement ConsoleOutputInterface');
71
        }
72
73
        $this->environment->registerListener(new OutputtingListener($output->getErrorOutput()));
74
75
        $commandBus = $this->environment->getCommandBus();
76
77
        try {
78
            $this->dispatcher->dispatch(new ExecutionStarted);
79
            $this->console->execute($input, $output);
80
            $commandBus->handle(new Commit);
81
            $this->dispatcher->dispatch(new ExecutionStopped);
82
        } catch (GiroappException $e) {
83
            $this->dispatcher->dispatch(
84
                new LogEvent(
85
                    $e->getMessage(),
86
                    [
87
                        'code' => $e->getCode(),
88
                    ],
89
                    LogLevel::ERROR
90
                )
91
            );
92
93
            $commandBus->handle(new Rollback);
94
95
            return $e->getCode();
96
        } catch (\Exception $e) {
97
            $this->dispatcher->dispatch(
98
                new LogEvent(
99
                    $e->getMessage(),
100
                    [
101
                        'class' => get_class($e),
102
                        'file' => $e->getFile(),
103
                        'line' => $e->getLine(),
104
                        'trace' => $e->getTraceAsString(),
105
                    ],
106
                    LogLevel::ERROR
107
                )
108
            );
109
110
            if ($output->isVerbose()) {
111
                $output->writeln($e->getTraceAsString());
112
            }
113
114
            $commandBus->handle(new Rollback);
115
116
            return $e->getCode() ?: GiroappException::GENERIC_ERROR;
117
        }
118
119
        return 0;
120
    }
121
}
122