Completed
Push — master ( dc181c...08db18 )
by recca
12s queued 10s
created

src/Middleware/RenderBar.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Recca0120\LaravelTracy\Middleware;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use Recca0120\LaravelTracy\DebuggerManager;
8
use Symfony\Component\HttpFoundation\Response;
9
use Recca0120\LaravelTracy\Events\BeforeBarRender;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\HttpFoundation\StreamedResponse;
12
use Symfony\Component\HttpFoundation\BinaryFileResponse;
13
14
class RenderBar
15
{
16
    /**
17
     * $debuggerManager.
18
     *
19
     * @var \Recca0120\LaravelTracy\DebuggerManager
20
     */
21
    protected $debuggerManager;
22
23
    /**
24
     * $events.
25
     *
26
     * @var \Illuminate\Contracts\Events\Dispatcher
27
     */
28
    protected $events;
29
30
    /**
31
     * __construct.
32
     *
33
     *
34
     * @param \Recca0120\LaravelTracy\DebuggerManager $debuggerManager
35
     * @param \Illuminate\Contracts\Events\Dispatcher $events
36
     */
37 9
    public function __construct(DebuggerManager $debuggerManager, Dispatcher $events)
38
    {
39 9
        $this->debuggerManager = $debuggerManager;
40 9
        $this->events = $events;
41 9
    }
42
43
    /**
44
     * handle.
45
     *
46
     * @param \Illuminate\Http\Request $request
47
     * @param \Closure $next
48
     * @return \Symfony\Component\HttpFoundation\Response
49
     */
50 9
    public function handle($request, $next)
51
    {
52 9
        return $request->has('_tracy_bar') === true
53 1
            ? $this->keepFlashSession($request, $next)
54 9
            : $this->render($request, $next);
55
    }
56
57
    /**
58
     * keepFlashSession.
59
     *
60
     * @param \Illuminate\Http\Request $request
61
     * @param \Closure $next
62
     * @return \Symfony\Component\HttpFoundation\Response
63
     */
64 1
    protected function keepFlashSession($request, $next)
65
    {
66 1
        $type = $request->get('_tracy_bar');
67 1
        if ($request->hasSession() === true && in_array($type, ['js', 'css'], true) === false) {
68 1
            $request->session()->reflash();
0 ignored issues
show
The method reflash() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
        }
70
71 1
        return $next($request);
72
    }
73
74
    /**
75
     * render.
76
     *
77
     * @param \Illuminate\Http\Request $request
78
     * @param \Closure $next
79
     * @return \Symfony\Component\HttpFoundation\Response
80
     */
81 8
    protected function render($request, $next)
82
    {
83 8
        $this->debuggerManager->dispatch();
84
85 8
        $response = $next($request);
86
87 8
        $ajax = $request->ajax();
88
89 8
        if ($this->reject($response, $request, $ajax) === true) {
90 4
            return $response;
91
        }
92
93 4
        $this->events->fire(new BeforeBarRender($request, $response));
94
95 4
        $response->setContent(
96 4
            $this->debuggerManager->shutdownHandler(
97 4
                $response->getContent(), $ajax
98
            )
99
        );
100
101 4
        return $response;
102
    }
103
104
    /**
105
     * reject.
106
     *
107
     * @param \Symfony\Component\HttpFoundation\Response $response
108
     * @param \Illuminate\Http\Request $request
109
     * @param bool $ajax
110
     *
111
     * @return bool
112
     */
113 8
    protected function reject(Response $response, Request $request, $ajax)
114
    {
115
        if (
116 8
            $response instanceof BinaryFileResponse ||
117 7
            $response instanceof StreamedResponse ||
118 8
            $response instanceof RedirectResponse
119
        ) {
120 3
            return true;
121
        }
122
123 5
        if ($ajax === true) {
124 1
            return false;
125
        }
126
127 4
        $contentType = strtolower($response->headers->get('Content-Type'));
128 4
        $accepts = $this->debuggerManager->accepts();
129 4
        if ((empty($contentType) === true && $response->getStatusCode() >= 400) ||
130 4
            count($accepts) === 0
131
        ) {
132 2
            return false;
133
        }
134
135 2
        foreach ($accepts as $accept) {
136 2
            if (strpos($contentType, $accept) !== false) {
137 2
                return false;
138
            }
139
        }
140
141 1
        return true;
142
    }
143
}
144