IfNoneMatch   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 15
c 2
b 1
f 0
dl 0
loc 44
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A stripWeakTags() 0 3 1
A handle() 0 28 4
1
<?php
2
3
namespace Werk365\EtagConditionals\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Werk365\EtagConditionals\EtagConditionals;
8
9
class IfNoneMatch extends Middleware
10
{
11
    public string $middleware = 'ifNoneMatch';
12
13
    /**
14
     * Handle an incoming request.
15
     *
16
     * @param  \Illuminate\Http\Request  $request
17
     * @param  \Closure  $next
18
     * @return mixed
19
     */
20
    public function handle(Request $request, Closure $next)
21
    {
22
        // Handle request
23
        $method = $request->getMethod();
24
25
        // Support using HEAD method for checking If-None-Match
26
        if ($request->isMethod('HEAD')) {
27
            $request->setMethod('GET');
28
        }
29
30
        //Handle response
31
        $response = $next($request);
32
33
        $etag = EtagConditionals::getEtag($request, $response);
34
        $noneMatch = $request->getETags();
35
36
        // Strip W/ if weak comparison algorithm can be used
37
        if (config('etagconditionals.if_none_match_weak')) {
38
            $noneMatch = array_map([$this, 'stripWeakTags'], $noneMatch);
39
        }
40
41
        if (in_array($etag, $noneMatch)) {
42
            $response->setNotModified();
43
        }
44
45
        $request->setMethod($method);
46
47
        return $response;
48
    }
49
50
    private function stripWeakTags($etag)
51
    {
52
        return str_replace('W/', '', $etag);
53
    }
54
}
55