Decorator   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 136
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B report() 0 24 5
A reporter() 0 4 1
A render() 0 10 3
A renderer() 0 4 1
A renderForConsole() 0 10 3
A consoleRenderer() 0 4 1
1
<?php
2
3
namespace RenePardon\GelfSupport;
4
5
use Closure;
6
use Exception;
7
use Illuminate\Config\Repository;
8
use Illuminate\Contracts\Debug\ExceptionHandler;
9
use Psr\Log\LogLevel;
10
use Symfony\Component\HttpFoundation\Response;
11
12
/**
13
 * Class Decorator
14
 *
15
 * @package RenePardon\GelfSupport
16
 */
17
class Decorator implements ExceptionHandler
18
{
19
    /**
20
     * @var ExceptionHandler $handler Laravel default exception handler.
21
     */
22
    protected $handler;
23
24
    /**
25
     * @var ExceptionHandlerRepository $handlers Custom exception handlers repository.
26
     */
27
    protected $handlers;
28
29
    /**
30
     * @var Repository
31
     */
32
    protected $config;
33
34
    /**
35
     * Set the dependencies.
36
     *
37
     * @param ExceptionHandler           $handler
38
     * @param ExceptionHandlerRepository $handlers
39
     */
40
    public function __construct(ExceptionHandler $handler, ExceptionHandlerRepository $handlers, Repository $config)
41
    {
42
        $this->handler = $handler;
43
        $this->handlers = $handlers;
44
        $this->config = $config;
45
    }
46
47
    /**
48
     * Report or log an exception.
49
     *
50
     * @param  \Exception $e
51
     *
52
     * @return void
53
     */
54
    public function report(Exception $e)
55
    {
56
        // Do the Graylog logging stuff if enabled
57
        if (true == $this->config->get('gelfsupport.enabled')) {
58
            $exceptionMessage = empty($e->getMessage()) ? 'no exception message set' : $e->getMessage();
59
60
            /** @var Graylog $graylog */
61
            $graylog = app(Graylog::class);
62
            $graylog->log(LogLevel::CRITICAL, $exceptionMessage, [
63
                'exception' => $e,
64
                'app_name'  => $this->config->get('app.name'),
65
            ]);
66
        }
67
68
        // Check for additional custom reporters/handlers
69
        foreach ($this->handlers->getReportersFor($e) as $reporter) {
70
            if ($report = $reporter($e)) {
71
                return $report;
72
            }
73
        }
74
75
        // Use the default report handler if no custom one was found
76
        $this->handler->report($e);
77
    }
78
79
    /**
80
     * Register a custom handler to report exceptions
81
     *
82
     * @param Closure $reporter
83
     *
84
     * @return int
85
     */
86
    public function reporter(Closure $reporter): int
87
    {
88
        return $this->handlers->addReporter($reporter);
89
    }
90
91
    /**
92
     * Render an exception into an HTTP response
93
     *
94
     * @param \Illuminate\Http\Request $request
95
     * @param Exception                $e
96
     *
97
     * @return Response
98
     */
99
    public function render($request, Exception $e): Response
100
    {
101
        foreach ($this->handlers->getRenderersFor($e) as $renderer) {
102
            if ($render = $renderer($e, $request)) {
103
                return $render;
104
            }
105
        }
106
107
        return $this->handler->render($request, $e);
108
    }
109
110
    /**
111
     * Register a custom handler to render exceptions
112
     *
113
     * @param Closure $renderer
114
     *
115
     * @return int
116
     */
117
    public function renderer(Closure $renderer): int
118
    {
119
        return $this->handlers->addRenderer($renderer);
120
    }
121
122
    /**
123
     * Render an exception to the console
124
     *
125
     * @param \Symfony\Component\Console\Output\OutputInterface $output
126
     * @param Exception                                         $e
127
     *
128
     * @return mixed
129
     */
130
    public function renderForConsole($output, Exception $e)
131
    {
132
        foreach ($this->handlers->getConsoleRenderersFor($e) as $renderer) {
133
            if ($render = $renderer($e, $output)) {
134
                return $render;
135
            }
136
        }
137
138
        $this->handler->renderForConsole($output, $e);
139
    }
140
141
    /**
142
     * Register a custom handler to render exceptions to the console
143
     *
144
     * @param Closure $renderer
145
     *
146
     * @return int
147
     */
148
    public function consoleRenderer(Closure $renderer): int
149
    {
150
        return $this->handlers->addConsoleRenderer($renderer);
151
    }
152
}
153