HtmlMinify::compressionPossible()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 19
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Abordage\LaravelHtmlMin\Middleware;
6
7
use Abordage\LaravelHtmlMin\Facades\HtmlMin;
8
use Closure;
9
use Illuminate\Http\Request;
10
use Illuminate\Http\Response;
11
12
class HtmlMinify
13
{
14
    /**
15
     * @param Request $request
16
     * @param Closure $next
17
     * @return Response|mixed
18
     */
19
    public function handle(Request $request, Closure $next)
20
    {
21
        $response = $next($request);
22
23
        if (!$this->compressionPossible($request, $response)) {
24
            return $response;
25
        }
26
27
        $html = $response->getContent();
28
29
        if (class_exists('\BeyondCode\ServerTiming\Facades\ServerTiming')) {
30
            \BeyondCode\ServerTiming\Facades\ServerTiming::start('Minification');
0 ignored issues
show
Bug introduced by
The type BeyondCode\ServerTiming\Facades\ServerTiming was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
        }
32
33
        $htmlMin = HtmlMin::minify($html);
34
35
        if (class_exists('\BeyondCode\ServerTiming\Facades\ServerTiming')) {
36
            \BeyondCode\ServerTiming\Facades\ServerTiming::stop('Minification');
37
        }
38
39
        return $response->setContent($htmlMin);
40
    }
41
42
    /**
43
     * @param Request $request
44
     * @param mixed $response
45
     * @return bool
46
     */
47
    private function compressionPossible(Request $request, $response): bool
48
    {
49
        if (!config('html-min.enable')) {
50
            return false;
51
        }
52
53
        if (!in_array(strtoupper($request->getMethod()), ['GET', 'HEAD'])) {
54
            return false;
55
        }
56
57
        if (!$response instanceof Response) {
58
            return false;
59
        }
60
61
        if ($response->getStatusCode() >= 500) {
62
            return false;
63
        }
64
65
        return true;
66
    }
67
}
68