Completed
Push — master ( 69db4e...f5c0e9 )
by recca
04:00 queued 01:26
created

RenderBar::appendBar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 20
ccs 12
cts 12
cp 1
crap 2
rs 9.4285
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 10
    public function __construct(DebuggerManager $debuggerManager, Dispatcher $events)
38
    {
39 10
        $this->debuggerManager = $debuggerManager;
40 10
        $this->events = $events;
41 10
    }
42
43
    /**
44
     * handle.
45
     *
46
     * @param \Illuminate\Http\Request $request
47
     * @param \Closure $next
48
     * @return \Symfony\Component\HttpFoundation\Response
49
     */
50 10
    public function handle($request, $next)
51
    {
52 10
        return $request->has('_tracy_bar') === true
53 10
            ? $this->renderBar($request, $next)
54 10
            : $this->appendBar($request, $next);
55
    }
56
57
    /**
58
     * appendBar.
59
     *
60
     * @param \Illuminate\Http\Request $request
61
     * @param \Closure $next
62
     * @return \Symfony\Component\HttpFoundation\Response
63
     */
64 1
    protected function renderBar($request, $next)
65
    {
66 1
        $response = $next($request);
67 1
        $type = $request->get('_tracy_bar');
68 1
        if ($request->hasSession() === true && in_array($type, ['js', 'css'], true) === false) {
69 1
            $request->session()->reflash();
70 1
        }
71
72 1
        return $response;
73
    }
74
75
    /**
76
     * appendBar.
77
     *
78
     * @param \Illuminate\Http\Request $request
79
     * @param \Closure $next
80
     * @return \Symfony\Component\HttpFoundation\Response
81
     */
82 9
    protected function appendBar($request, $next)
83
    {
84 9
        $this->debuggerManager->dispatch();
85
86 9
        $response = $next($request);
87
88 9
        if ($this->shouldNotRenderBar($response, $request) === true) {
89 5
            return $response;
90
        }
91
92 4
        $this->events->fire(new BeforeBarRender($request, $response));
93
94 4
        $response->setContent(
95 4
            $this->debuggerManager->shutdownHandler(
96 4
                $response->getContent()
97 4
            )
98 4
        );
99
100 4
        return $response;
101
    }
102
103
    /**
104
     * shouldNotRenderBar.
105
     *
106
     * @param \Symfony\Component\HttpFoundation\Response $response
107
     * @param \Illuminate\Http\Request $request
108
     *
109
     * @return bool
110
     */
111 9
    protected function shouldNotRenderBar(Response $response, Request $request)
112
    {
113 9
        if ($this->debuggerManager->showBar() === false ||
114 8
            $response instanceof BinaryFileResponse ||
0 ignored issues
show
Bug introduced by
The class Symfony\Component\HttpFo...tion\BinaryFileResponse does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
115 7
            $response instanceof StreamedResponse ||
0 ignored issues
show
Bug introduced by
The class Symfony\Component\HttpFoundation\StreamedResponse does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
116
            $response instanceof RedirectResponse
0 ignored issues
show
Bug introduced by
The class Symfony\Component\HttpFoundation\RedirectResponse does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
117 9
        ) {
118 4
            return true;
119
        }
120
121 5
        if ($request->ajax() === true) {
122 1
            return false;
123
        }
124
125 4
        $contentType = strtolower($response->headers->get('Content-Type'));
126 4
        $accepts = $this->debuggerManager->accepts();
127 4
        if ((empty($contentType) === true && $response->getStatusCode() >= 400) ||
128 3
            count($accepts) === 0
129 4
        ) {
130 2
            return false;
131
        }
132
133 2
        foreach ($accepts as $accept) {
134 2
            if (strpos($contentType, $accept) !== false) {
135 1
                return false;
136
            }
137 1
        }
138
139 1
        return true;
140
    }
141
}
142