Completed
Branch geo-attribute (e765b6)
by Raed
04:45
created

GeoRoutesMiddleware::handle()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 2
dl 0
loc 20
ccs 9
cts 10
cp 0.9
crap 5.025
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace LaraCrafts\GeoRoutes\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use LaraCrafts\GeoRoutes\DeterminesGeoAccess;
8
9
class GeoRoutesMiddleware
10
{
11
    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...
12
13
    /**
14
     * Handle an incoming request.
15
     *
16
     * @param \Illuminate\Http\Request $request
17
     * @param \Closure $next
18
     * @param string $strategy
19
     * @param string $countries
20
     * @param string|null $callback
21
     *
22
     * @return mixed
23
     */
24 72
    public function handle(Request $request, Closure $next)
25
    {
26 72
        $route = $request->route();
27
28 72
        if (!$route) {
29
            #TODO: Invoke the default callback.
30
            return abort(401);
1 ignored issue
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...
31
        }
32
33 72
        $geo = $route->getAction('geo');
34
35 72
        if ($this->shouldHaveAccess($request, (array)$geo['countries'], $geo['strategy'])) {
36 12
            return $next($request);
37
        }
38
39 60
        if (array_key_exists('callback', $geo) && $callback = $geo['callback']) {
0 ignored issues
show
Bug introduced by
It seems like $geo can also be of type null; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

39
        if (array_key_exists('callback', /** @scrutinizer ignore-type */ $geo) && $callback = $geo['callback']) {
Loading history...
40 36
            return call_user_func_array($callback[0], $callback[1]);
41
        }
42
43 24
        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...
44
    }
45
}
46