Completed
Pull Request — master (#157)
by Hannes
02:30
created

Adapter::execute()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
nc 5
nop 2
dl 0
loc 31
rs 9.6666
c 0
b 0
f 0
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-18 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\giroapp\Console;
24
25
use byrokrat\giroapp\Events;
26
use byrokrat\giroapp\Event\LogEvent;
27
use byrokrat\giroapp\Listener\OutputtingSubscriber;
28
use byrokrat\giroapp\Listener\ExitStatusSubscriber;
29
use Symfony\Component\Console\Command\Command as SymfonyCommand;
30
use Symfony\Component\Console\Input\InputInterface;
31
use Symfony\Component\Console\Output\ConsoleOutputInterface;
32
use Symfony\Component\Console\Output\OutputInterface;
33
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
34
35
final class Adapter extends SymfonyCommand
36
{
37
    /**
38
     * @var CommandInterface
39
     */
40
    private $command;
41
42
    /**
43
     * @var EventDispatcherInterface
44
     */
45
    private $dispatcher;
46
47
    public function __construct(CommandInterface $command, EventDispatcherInterface $dispatcher)
48
    {
49
        $this->command = $command;
50
        $this->dispatcher = $dispatcher;
51
        parent::__construct();
52
    }
53
54
    protected function configure(): void
55
    {
56
        $this->command->configure($this);
57
    }
58
59
    protected function execute(InputInterface $input, OutputInterface $output): int
60
    {
61
        if (!$output instanceof ConsoleOutputInterface) {
62
            throw new \InvalidArgumentException('Output must implement ConsoleOutputInterface');
63
        }
64
65
        $exitStatus = new ExitStatusSubscriber;
66
67
        $this->dispatcher->addSubscriber($exitStatus);
68
69
        $this->dispatcher->addSubscriber(new OutputtingSubscriber($output, $output->getErrorOutput()));
70
71
        try {
72
            $this->dispatcher->dispatch(Events::EXECUTION_STARTED, new LogEvent('Execution started'));
73
            $this->command->execute($input, $output);
74
            $this->dispatcher->dispatch(Events::EXECUTION_STOPED, new LogEvent('Execution successful'));
75
        } catch (\Exception $e) {
76
            $this->dispatcher->dispatch(
77
                Events::ERROR,
78
                new LogEvent(
79
                    "[EXCEPTION] {$e->getMessage()}",
80
                    [
81
                        'class' => get_class($e),
82
                        'file' => $e->getFile(),
83
                        'line' => $e->getLine()
84
                    ]
85
                )
86
            );
87
        }
88
89
        return $exitStatus->getExitStatus();
90
    }
91
}
92