Passed
Push — main ( c1e8d7...13f864 )
by Sebastian
03:58
created

Command::crash()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 3
rs 10
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Console;
13
14
use CaptainHook\App\Console\Runtime\Resolver;
15
use Symfony\Component\Console\Command\Command as SymfonyCommand;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Throwable;
19
20
/**
21
 * Class Command
22
 *
23
 * @package CaptainHook
24
 * @author  Sebastian Feldmann <[email protected]>
25
 * @link    https://github.com/captainhook-git/captainhook
26
 * @since   Class available since Release 5.0.0
27
 */
28
abstract class Command extends SymfonyCommand
29
{
30
    /**
31
     * Input output handler
32
     *
33
     * @var \CaptainHook\App\Console\IO
34
     */
35
    private $io;
36
37
    /**
38
     * Runtime resolver
39
     *
40
     * @var \CaptainHook\App\Console\Runtime\Resolver
41
     */
42
    protected Resolver $resolver;
43
44
    /**
45
     * Command constructor
46
     *
47
     * @param \CaptainHook\App\Console\Runtime\Resolver $resolver
48
     */
49 43
    public function __construct(Resolver $resolver)
50
    {
51 43
        $this->resolver = $resolver;
52 43
        parent::__construct();
53
    }
54
55
    /**
56
     * IO setter
57
     *
58
     * @param \CaptainHook\App\Console\IO $io
59
     */
60 7
    public function setIO(IO $io): void
61
    {
62 7
        $this->io = $io;
63
    }
64
65
    /**
66
     * IO interface getter
67
     *
68
     * @param  \Symfony\Component\Console\Input\InputInterface   $input
69
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
70
     * @return \CaptainHook\App\Console\IO
71
     */
72 40
    public function getIO(InputInterface $input, OutputInterface $output): IO
73
    {
74 40
        if (null === $this->io) {
75 33
            $this->io = new IO\DefaultIO($input, $output, $this->getHelperSet());
76
        }
77 40
        return $this->io;
78
    }
79
80
    /**
81
     * Write a final error message
82
     *
83
     * @param  \Symfony\Component\Console\Output\OutputInterface $out
84
     * @param  \Throwable                                        $t
85
     * @return int
86
     * @throws \Throwable
87
     */
88 18
    public function crash(OutputInterface $out, Throwable $t): int
89
    {
90 18
        if ($out->isDebug()) {
91 1
            throw $t;
92
        }
93
94 17
        $out->writeln('<fg=red>' . $t->getMessage() . '</>');
95 17
        if ($out->isVerbose()) {
96 1
            $out->writeln(
97 1
                '<comment>Error triggered in file:</comment> ' . $t->getFile() .
98 1
                ' <comment>in line:</comment> ' . $t->getLine()
99 1
            );
100
        }
101 17
        return 1;
102
    }
103
}
104