Passed
Pull Request — master (#69)
by Dave
02:21
created

ErrorReporter::reportError()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 20
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 29
rs 8.6666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DaveLiddament\StaticAnalysisResultsBaseliner\Framework\Command\internal;
6
7
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\BaseLiner\BaseLineImportException;
8
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\Common\SarbException;
9
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\File\FileAccessException;
10
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\HistoryAnalyser\HistoryAnalyserException;
11
use DaveLiddament\StaticAnalysisResultsBaseliner\Domain\ResultsParser\AnalysisResultsImportException;
12
use Symfony\Component\Console\Output\ConsoleOutputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Throwable;
15
16
class ErrorReporter
17
{
18
    public static function reportError(OutputInterface $output, Throwable $throwable): int
19
    {
20
        try {
21
            throw $throwable;
22
        } catch (InvalidConfigException $e) {
23
            self::writeToStdError($output, $e->getMessage());
24
25
            return 11;
26
        } catch (BaseLineImportException $e) {
27
            self::writeToStdError($output, $e->getMessage());
28
29
            return 12;
30
        } catch (AnalysisResultsImportException $e) {
31
            self::writeToStdError($output, $e->getMessage());
32
33
            return 13;
34
        } catch (FileAccessException $e) {
35
            self::writeToStdError($output, $e->getMessage());
36
37
            return 14;
38
        } catch (HistoryAnalyserException $e) {
39
            self::writeToStdError($output, $e->getMessage());
40
41
            return 6;
42
        } catch (Throwable $e) {
43
            // This should never happen. All exceptions should extend SarbException
44
            self::writeToStdError($output, "Unexpected critical error: {$e->getMessage()}");
45
46
            return 100;
47
        }
48
    }
49
50
    public static function writeToStdError(OutputInterface $output, string $message): void
51
    {
52
        $errorOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
53
        $errorOutput->writeln("<error>$message</error>");
54
    }
55
}
56