Completed
Pull Request — master (#56)
by
unknown
17:43
created

Handler::shouldRenderException()   A

Complexity

Conditions 6
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 2
nop 1
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Recca0120\LaravelTracy\Exceptions;
4
5
use Exception;
6
use Illuminate\Contracts\Debug\ExceptionHandler;
7
use Illuminate\Contracts\View\View;
8
use Illuminate\Http\Response;
9
use Recca0120\LaravelTracy\DebuggerManager;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Throwable;
13
14
class Handler implements ExceptionHandler
15
{
16
    /**
17
     * app exception handler.
18
     *
19
     * @var \Illuminate\Contracts\Debug\ExceptionHandler
20
     */
21
    protected $exceptionHandler;
22
23
    /**
24
     * $debuggerManager.
25
     *
26
     * @var \Recca0120\LaravelTracy\DebuggerManager
27
     */
28
    protected $debuggerManager;
29
30
    /**
31
     * __construct.
32
     *
33
     * @param \Illuminate\Contracts\Debug\ExceptionHandler $exceptionHandler
34
     * @param \Recca0120\LaravelTracy\DebuggerManager $debuggerManager
35 12
     */
36
    public function __construct(ExceptionHandler $exceptionHandler, DebuggerManager $debuggerManager)
37 12
    {
38 12
        $this->exceptionHandler = $exceptionHandler;
39 12
        $this->debuggerManager = $debuggerManager;
40
    }
41
42
    /**
43
     * Report or log an exception.
44
     *
45
     * @param  \Throwable  $e
46 1
     * @return void
47
     *
48 1
     * @throws \Exception
49 1
     */
50
    public function report(Throwable $e)
51
    {
52
        $this->exceptionHandler->report($e);
53
    }
54
55
    /**
56
     * Determine if the exception should be reported.
57 1
     *
58
     * @param  \Throwable  $e
59 1
     * @return bool
60
     */
61
    public function shouldReport(Throwable $e)
62
    {
63
        return $this->exceptionHandler->shouldReport($e);
64
    }
65
66
    /**
67
     * Render an exception into an HTTP response.
68
     *
69 8
     * @param  \Illuminate\Http\Request  $request
70
     * @param  \Throwable  $e
71 8
     * @return \Symfony\Component\HttpFoundation\Response
72
     *
73 8
     * @throws \Throwable
74 1
     */
75 1
    public function render($request, Throwable $e)
76 1
    {
77
        $response = $this->exceptionHandler->render($request, $e);
78
79
        if ($this->shouldRenderException($response) === true) {
80 8
            $_SERVER = $request->server();
81
            $response->setContent(
82
                $this->debuggerManager->exceptionHandler($e)
83
            );
84
        }
85
86
        return $response;
87
    }
88
89 1
    /**
90
     * Render an exception to the console.
91 1
     *
92 1
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
93
     * @param  \Throwable  $e
94
     * @return void
95
     */
96
    public function renderForConsole($output, Throwable $e)
97
    {
98
        $this->exceptionHandler->renderForConsole($output, $e);
99
    }
100 8
101
    /**
102 8
     * shouldRenderException.
103 2
     *
104
     * @param \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response $response
105
     * @return bool
106 6
     */
107 2
    protected function shouldRenderException($response)
108
    {
109
        if (
110 4
            $response instanceof RedirectResponse ||
111 2
            $response instanceof JsonResponse ||
112
            $response->getContent() instanceof View ||
113
            ($response instanceof Response && $response->getOriginalContent() instanceof View)
114 2
        ) {
115 1
            return false;
116
        }
117
118 1
        return true;
119
    }
120
}
121