Completed
Branch master (10c809)
by Andrea Marco
07:44 queued 05:58
created

Decorator::consoleRenderer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Cerbero\ExceptionHandler;
4
5
use Closure;
6
use Exception;
7
use Illuminate\Contracts\Debug\ExceptionHandler;
8
9
/**
10
 * Decorator for the Laravel default exception handler.
11
 *
12
 * @author    Andrea Marco Sartori
13
 */
14
class Decorator implements ExceptionHandler
15
{
16
    /**
17
     * @author    Andrea Marco Sartori
18
     * @var        Illuminate\Contracts\Debug\ExceptionHandler    $handler    Laravel default exception handler.
19
     */
20
    protected $handler;
21
22
    /**
23
     * @author    Andrea Marco Sartori
24
     * @var        Cerbero\ExceptionHandler\Repository    $handlers    Custom exception handlers repository.
25
     */
26
    protected $handlers;
27
28
    /**
29
     * Set the dependencies.
30
     *
31
     * @author    Andrea Marco Sartori
32
     * @param    Illuminate\Contracts\Debug\ExceptionHandler    $handler
33
     * @param    Cerbero\ExceptionHandler\Repository    $handlers
34
     * @return    void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
35
     */
36 21
    public function __construct(ExceptionHandler $handler, Repository $handlers)
37
    {
38 21
        $this->handler  = $handler;
0 ignored issues
show
Documentation Bug introduced by
It seems like $handler of type object<Illuminate\Contra...Debug\ExceptionHandler> is incompatible with the declared type object<Cerbero\Exception...Debug\ExceptionHandler> of property $handler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
40 21
        $this->handlers = $handlers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $handlers of type object<Cerbero\ExceptionHandler\Repository> is incompatible with the declared type object<Cerbero\Exception...tionHandler\Repository> of property $handlers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41 21
    }
42
43
    /**
44
     * Report or log an exception.
45
     *
46
     * @param  \Exception  $e
47
     * @return void
48
     */
49 3
    public function report(Exception $e)
50
    {
51 3
        foreach ($this->handlers->getReportersFor($e) as $reporter) {
52 3
            if ($report = $reporter($e)) {
53
                return $report;
54
            }
55 3
        }
56
57 3
        $this->handler->report($e);
58 3
    }
59
60
    /**
61
     * Register a custom handler to report exceptions.
62
     *
63
     * @author    Andrea Marco Sartori
64
     * @param    \Closure    $reporter
65
     * @return    integer
66
     */
67 3
    public function reporter(Closure $reporter)
68
    {
69 3
        return $this->handlers->addReporter($reporter);
70
    }
71
72
    /**
73
     * Render an exception into an HTTP response.
74
     *
75
     * @param  \Illuminate\Http\Request  $request
76
     * @param  \Exception  $e
77
     * @return \Symfony\Component\HttpFoundation\Response
78
     */
79 3
    public function render($request, Exception $e)
80
    {
81 3
        foreach ($this->handlers->getRenderersFor($e) as $renderer) {
82 3
            if ($render = $renderer($e, $request)) {
83
                return $render;
84
            }
85 3
        }
86
87 3
        return $this->handler->render($request, $e);
88
    }
89
90
    /**
91
     * Register a custom handler to render exceptions.
92
     *
93
     * @author    Andrea Marco Sartori
94
     * @param    \Closure    $renderer
95
     * @return    integer
96
     */
97 3
    public function renderer(Closure $renderer)
98
    {
99 3
        return $this->handlers->addRenderer($renderer);
100
    }
101
102
    /**
103
     * Render an exception to the console.
104
     *
105
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
106
     * @param  \Exception  $e
107
     * @return void
108
     */
109 3
    public function renderForConsole($output, Exception $e)
110
    {
111 3
        foreach ($this->handlers->getConsoleRenderersFor($e) as $renderer) {
112 3
            if ($render = $renderer($e, $output)) {
113
                return $render;
114
            }
115 3
        }
116
117 3
        $this->handler->renderForConsole($output, $e);
118 3
    }
119
120
    /**
121
     * Register a custom handler to render exceptions to the console.
122
     *
123
     * @author    Andrea Marco Sartori
124
     * @param    \Closure    $renderer
125
     * @return    integer
126
     */
127 3
    public function consoleRenderer(Closure $renderer)
128
    {
129 3
        return $this->handlers->addConsoleRenderer($renderer);
130
    }
131
}
132