Test Failed
Push — master ( de1e02...a77b5e )
by Pavel
09:35
created

HtmlMinify::handle()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 2
b 0
f 0
nc 8
nop 2
dl 0
loc 34
rs 8.8333
1
<?php
2
3
namespace Abordage\LaravelHtmlMin\Middleware;
4
5
use Abordage\LaravelHtmlMin\Facades\HtmlMin;
6
use Closure;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\Response;
9
10
class HtmlMinify
11
{
12
    /**
13
     * @param Request $request
14
     * @param Closure $next
15
     * @return Response|mixed
16
     */
17
    public function handle(Request $request, Closure $next)
18
    {
19
        $response = $next($request);
20
21
        if (!config('html-min.enable')) {
22
            return $response;
23
        }
24
25
        if (!in_array(strtoupper($request->getMethod()), ['GET', 'HEAD'])) {
26
            return $response;
27
        }
28
29
        if (!$response instanceof Response) {
30
            return $response;
31
        }
32
33
        if (!$response->getStatusCode() != 200) {
34
            return $response;
35
        }
36
37
        $html = $response->getContent();
38
39
        if (class_exists('\BeyondCode\ServerTiming\Facades\ServerTiming')) {
40
            \BeyondCode\ServerTiming\Facades\ServerTiming::start('Minification');
41
        }
42
43
        /** @phpstan-ignore-next-line */
44
        $htmlMin = HtmlMin::minify($html);
45
46
        if (class_exists('\BeyondCode\ServerTiming\Facades\ServerTiming')) {
47
            \BeyondCode\ServerTiming\Facades\ServerTiming::stop('Minification');
48
        }
49
50
        return $response->setContent($htmlMin);
51
    }
52
}
53