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

ErrorReporter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A reportError() 0 21 5
A writeToStdError() 0 4 2
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 Symfony\Component\Console\Output\ConsoleOutputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Throwable;
12
13
class ErrorReporter
14
{
15
    public static function reportError(OutputInterface $output, Throwable $throwable): int
16
    {
17
        try {
18
            throw $throwable;
19
        } catch (InvalidConfigException $e) {
20
            self::writeToStdError($output, "<error>{$e->getMessage()}</error>");
21
22
            return 2;
23
        } catch (BaseLineImportException $e) {
24
            self::writeToStdError($output, "<error>{$e->getMessage()}</error>");
25
26
            return 3;
27
        } catch (SarbException $e) {
28
            self::writeToStdError($output, "<error>Something went wrong: {$e->getMessage()}");
29
30
            return 4;
31
        } catch (Throwable $e) {
32
            // This should never happen. All exceptions should extend SarbException
33
            self::writeToStdError($output, "<error>Unexpected critical error: {$e->getMessage()}</error>");
34
35
            return 5;
36
        }
37
    }
38
39
    public static function writeToStdError(OutputInterface $output, string $message): void
40
    {
41
        $errorOutput = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output;
42
        $errorOutput->writeln($message);
43
    }
44
}
45