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

HtmlMinify   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 41
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 34 7
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