GeoRoutesMiddleware::handle()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 5
b 0
f 0
nc 5
nop 2
dl 0
loc 30
ccs 13
cts 14
cp 0.9286
crap 6.0131
rs 9.2222
1
<?php
2
3
namespace LaraCrafts\GeoRoutes\Http\Middleware;
4
5
use Closure;
6
use Exception;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Validator;
9
use LaraCrafts\GeoRoutes\DeterminesGeoAccess;
10
11
class GeoRoutesMiddleware
12
{
13
    use DeterminesGeoAccess;
0 ignored issues
show
Bug introduced by
The trait LaraCrafts\GeoRoutes\DeterminesGeoAccess requires the property $countryCode which is not provided by LaraCrafts\GeoRoutes\Htt...are\GeoRoutesMiddleware.
Loading history...
14
15
    /**
16
     * Handle an incoming request.
17
     *
18
     * @param \Illuminate\Http\Request $request
19
     * @param \Closure $next
20
     *
21
     * @return mixed
22
     */
23 160
    public function handle(Request $request, Closure $next)
24
    {
25 160
        $route = $request->route();
26
27 160
        if (!$route) {
28
            #TODO: Invoke the default callback.
29
            return abort(401);
30
        }
31
32 160
        $constraint = $route->getAction('geo') ?? [];
33
34 160
        $validator = Validator::make($constraint, [
35 160
            'countries' => 'required|array|min:1',
36
            'countries.*' => 'string|min:2|max:2',
37
            'strategy' => 'required|in:allow,deny',
38
        ]);
39
40 160
        if ($validator->fails()) {
41 16
            throw new Exception("The GeoRoute constraint is invalid.");
42
        }
43
44 144
        if ($this->shouldHaveAccess((array)$constraint['countries'], $constraint['strategy'])) {
45 16
            return $next($request);
46
        }
47
48 128
        if (array_key_exists('callback', $constraint) && $callback = $constraint['callback']) {
49 80
            return call_user_func_array($callback[0], $callback[1]);
50
        }
51
52 48
        return abort(401);
0 ignored issues
show
Bug introduced by
Are you sure the usage of abort(401) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
53
    }
54
}
55