IfMatch::handle()   B
last analyzed

Complexity

Conditions 10
Paths 18

Size

Total Lines 50
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 4 Features 0
Metric Value
cc 10
eloc 24
c 8
b 4
f 0
nc 18
nop 2
dl 0
loc 50
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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