HandlerDecorator::shouldReport()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Cerbero\ExceptionHandler;
4
5
use Throwable;
6
use Illuminate\Contracts\Debug\ExceptionHandler;
7
8
/**
9
 * The exception handler decorator.
10
 *
11
 */
12
class HandlerDecorator implements ExceptionHandler
13
{
14
    /**
15
     * The default Laravel exception handler.
16
     *
17
     * @var \Illuminate\Contracts\Debug\ExceptionHandler
18
     */
19
    protected $defaultHandler;
20
21
    /**
22
     * The custom handlers repository.
23
     *
24
     * @var \Cerbero\ExceptionHandler\HandlersRepository
25
     */
26
    protected $repository;
27
28
    /**
29
     * Set the dependencies.
30
     *
31
     * @param \Illuminate\Contracts\Debug\ExceptionHandler $defaultHandler
32
     * @param \Cerbero\ExceptionHandler\HandlersRepository $repository
33
     */
34 33
    public function __construct(ExceptionHandler $defaultHandler, HandlersRepository $repository)
35
    {
36 33
        $this->defaultHandler = $defaultHandler;
37
38 33
        $this->repository = $repository;
39 33
    }
40
41
    /**
42
     * Report or log an exception.
43
     *
44
     * @param \Throwable $e
45
     * @return mixed
46
     *
47
     * @throws \Throwable
48
     */
49 15
    public function report(Throwable $e)
50
    {
51 15
        foreach ($this->repository->getReportersByException($e) as $reporter) {
52 9
            if ($report = $reporter($e)) {
53 9
                return $report;
54
            }
55
        }
56
57 6
        $this->defaultHandler->report($e);
58 6
    }
59
60
    /**
61
     * Register a custom handler to report exceptions
62
     *
63
     * @param callable $callback
64
     * @return int
65
     */
66 12
    public function reporter(callable $reporter)
67
    {
68 12
        return $this->repository->addReporter($reporter);
69
    }
70
71
    /**
72
     * Render an exception into a response.
73
     *
74
     * @param \Illuminate\Http\Request  $request
75
     * @param \Throwable $e
76
     * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
77
     */
78 6
    public function render($request, Throwable $e)
79
    {
80 6
        foreach ($this->repository->getRenderersByException($e) as $renderer) {
81 3
            if ($render = $renderer($e, $request)) {
82 3
                return $render;
83
            }
84
        }
85
86 3
        return $this->defaultHandler->render($request, $e);
87
    }
88
89
    /**
90
     * Register a custom handler to render exceptions
91
     *
92
     * @param callable $callback
93
     * @return int
94
     */
95 3
    public function renderer(callable $renderer)
96
    {
97 3
        return $this->repository->addRenderer($renderer);
98
    }
99
100
    /**
101
     * Render an exception to the console.
102
     *
103
     * @param \Symfony\Component\Console\Output\OutputInterface $output
104
     * @param \Throwable $e
105
     * @return mixed
106
     */
107 6
    public function renderForConsole($output, Throwable $e)
108
    {
109 6
        foreach ($this->repository->getConsoleRenderersByException($e) as $renderer) {
110 3
            if ($render = $renderer($e, $output)) {
111 3
                return $render;
112
            }
113
        }
114
115 3
        $this->defaultHandler->renderForConsole($output, $e);
116 3
    }
117
118
    /**
119
     * Register a custom handler to render exceptions in console
120
     *
121
     * @param callable $callback
122
     * @return int
123
     */
124 3
    public function consoleRenderer(callable $renderer)
125
    {
126 3
        return $this->repository->addConsoleRenderer($renderer);
127
    }
128
129
    /**
130
     * Determine if the exception should be reported.
131
     *
132
     * @param \Throwable $e
133
     * @return bool
134
     */
135 3
    public function shouldReport(Throwable $e)
136
    {
137 3
        return $this->defaultHandler->shouldReport($e);
138
    }
139
140
    /**
141
     * Proxy other calls to default Laravel exception handler
142
     *
143
     * @param string $name
144
     * @param array $parameters
145
     * @return mixed
146
     */
147 3
    public function __call($name, $parameters)
148
    {
149 3
        return call_user_func_array([$this->defaultHandler, $name], $parameters);
150
    }
151
}
152