Passed
Pull Request — master (#29)
by Raed
08:15
created

GeoRoutesMiddleware::handle()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7.6393

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 9
cts 14
cp 0.6429
crap 7.6393
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
     * @param string $strategy
21
     * @param string $countries
22
     * @param string|null $callback
23
     *
24
     * @return mixed
25
     */
26 84
    public function handle(Request $request, Closure $next)
27
    {
28 84
        $route = $request->route();
29
30 84
        if (!$route) {
31
            #TODO: Invoke the default callback.
32
            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...
33
        }
34
35 84
        $constraint = $route->getAction('geo') ?? [];
36
37 84
        $validator = Validator::make($constraint, [
38 84
            'countries' => 'required|array|min:1',
39
            'countries.*' => 'string|min:2|max:2',
40
            'strategy' => 'required|in:allow,deny',
41
        ]);
42
43 84
        if ($validator->fails()) {
44 12
            throw new Exception("The GeoRoute constraint is invalid.");
45
        }
46
47 72
        if ($this->shouldHaveAccess($request, (array)$constraint['countries'], $constraint['strategy'])) {
0 ignored issues
show
Bug introduced by
(array)$constraint['countries'] of type array is incompatible with the type string expected by parameter $strategy of LaraCrafts\GeoRoutes\Htt...are::shouldHaveAccess(). ( Ignorable by Annotation )

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

47
        if ($this->shouldHaveAccess($request, /** @scrutinizer ignore-type */ (array)$constraint['countries'], $constraint['strategy'])) {
Loading history...
Unused Code introduced by
The call to LaraCrafts\GeoRoutes\Htt...are::shouldHaveAccess() has too many arguments starting with $constraint['strategy']. ( Ignorable by Annotation )

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

47
        if ($this->/** @scrutinizer ignore-call */ shouldHaveAccess($request, (array)$constraint['countries'], $constraint['strategy'])) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
$request of type Illuminate\Http\Request is incompatible with the type array expected by parameter $countries of LaraCrafts\GeoRoutes\Htt...are::shouldHaveAccess(). ( Ignorable by Annotation )

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

47
        if ($this->shouldHaveAccess(/** @scrutinizer ignore-type */ $request, (array)$constraint['countries'], $constraint['strategy'])) {
Loading history...
48
            return $next($request);
49
        }
50
51
        if (array_key_exists('callback', $constraint) && $callback = $constraint['callback']) {
52
            return call_user_func_array($callback[0], $callback[1]);
53
        }
54
55
        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...
56
    }
57
}
58