IfMatch   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 8
Bugs 4 Features 0
Metric Value
eloc 26
c 8
b 4
f 0
dl 0
loc 61
rs 10
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 50 10
1
<?php
2
3
namespace Werk365\EtagConditionals\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
use Werk365\EtagConditionals\EtagConditionals;
9
10
class IfMatch extends Middleware
11
{
12
    public string $middleware = 'ifMatch';
13
14
    /**
15
     * Handle an incoming request.
16
     *
17
     * @param  \Illuminate\Http\Request  $request
18
     * @param  \Closure  $next
19
     * @return mixed
20
     */
21
    public function handle(Request $request, Closure $next)
22
    {
23
        // Next unless method is PATCH and If-Match header is set
24
        if (! ($request->isMethod('PATCH') && $request->hasHeader('If-Match'))) {
25
            return $next($request);
26
        }
27
28
        // Create new GET request to same endpoint,
29
        // copy headers and add header that allows you to ignore this request in middlewares
30
        $getRequest = Request::create($request->getRequestUri(), 'GET');
31
        $getRequest->headers = $request->headers;
32
        $getRequest->headers->set('X-From-Middleware', 'IfMatch');
33
        $getResponse = app()->handle($getRequest);
0 ignored issues
show
introduced by
The method handle() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        $getResponse = app()->/** @scrutinizer ignore-call */ handle($getRequest);
Loading history...
34
35
        // Get content from response object and get hashes from content and etag
36
        $getEtag = EtagConditionals::getEtag($request, $getResponse);
37
        $ifMatch = $request->header('If-Match');
38
39
        if ($ifMatch === null) {
40
            return response(null, 412);
41
        }
42
43
        $ifMatchArray = (is_string($ifMatch)) ?
44
            explode(',', $ifMatch) :
45
            $ifMatch;
46
47
        // Strip W/ if weak comparison algorithm can be used
48
        if (config('etagconditionals.if_match_weak')) {
49
            foreach ($ifMatchArray as &$match) {
50
                $match = str_replace('W/', '', $match);
51
            }
52
            unset($match);
53
        }
54
55
        foreach ($ifMatchArray as &$match) {
56
            $match = trim($match);
57
        }
58
        unset($match);
59
60
        // Compare current and request hashes
61
        // Also allow wildcard (*) values
62
        if (! (in_array($getEtag, $ifMatchArray) || in_array('"*"', $ifMatchArray))) {
63
            return response(null, 412);
64
        }
65
66
        // Before continuing, prepare the application to receive our request
67
        // This is for Laravel Octane support
68
        app()->instance('request', $request);
69
70
        return $next($request);
71
    }
72
}
73