RouteNeedsRole   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 11
dl 0
loc 32
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 22 4
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
7
/**
8
 * Class RouteNeedsRole.
9
 */
10
class RouteNeedsRole
11
{
12
    /**
13
     * @param $request
14
     * @param Closure $next
15
     * @param $role
16
     * @param bool $needsAll
17
     *
18
     * @return mixed
19
     */
20
    public function handle($request, Closure $next, $role, $needsAll = false)
21
    {
22
        /*
23
         * Roles array
24
         */
25
        if (strpos($role, ';') !== false) {
26
            $roles  = explode(';', $role);
27
            $access = access()->hasRoles($roles, ($needsAll === 'true' ? true : false));
0 ignored issues
show
introduced by
The condition $needsAll === 'true' is always false.
Loading history...
Bug introduced by
The method hasRoles() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

27
            $access = access()->/** @scrutinizer ignore-call */ hasRoles($roles, ($needsAll === 'true' ? true : false));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28
        } else {
29
            /**
30
             * Single role.
31
             */
32
            $access = access()->hasRole($role);
0 ignored issues
show
Bug introduced by
The method hasRole() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

32
            $access = access()->/** @scrutinizer ignore-call */ hasRole($role);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
33
        }
34
35
        if (! $access) {
36
            return redirect()
37
                ->route(homeRoute())
38
                ->withFlashDanger(trans('backpack::crud.unauthorized_access'));
39
        }
40
41
        return $next($request);
42
    }
43
}
44