Completed
Push — master ( b96870...b9bbf6 )
by Dusan
11s
created

AbstractEmitter::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WeCodeIn\ErrorHandling\Emitter;
6
7
use Throwable;
8
9
abstract class AbstractEmitter implements EmitterInterface
10
{
11
    /**
12
     * @var bool
13
     */
14
    protected $includeTrace;
15
16 3
    public function __construct(bool $includeTrace = true)
17
    {
18 3
        $this->includeTrace = $includeTrace;
19 3
    }
20
21 3
    public function __invoke(Throwable $throwable) : Throwable
22
    {
23 3
        ob_start();
24 3
        $output = $this->format($throwable);
25 3
        ob_end_clean();
26
27 3
        echo $output;
28
29 3
        return $throwable;
30
    }
31
32
    abstract protected function format(Throwable $throwable) : string;
33
34
    final protected function formatTraceRecord(array $traceRecord, int $index, int $traceLength) : string
35
    {
36
        return sprintf(
37
            '#%s %s%s%s in %s:%s',
38
            $traceLength - $index - 1,
39
            $traceRecord['class'] ?? '',
40
            isset($traceRecord['class'], $traceRecord['function']) ? ':' : '',
41
            $traceRecord['function'] ?? '',
42
            $traceRecord['file'] ?? 'unknown',
43
            $traceRecord['line'] ?? 0
44
        );
45
    }
46
}
47