RoleMiddleware::handle()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 6
nop 3
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pratiksh\Adminetic\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\Auth;
8
9
class RoleMiddleware
10
{
11
    /**
12
     * Handle an incoming request.
13
     *
14
     * @param  \Illuminate\Http\Request  $request
15
     * @param  \Closure  $next
16
     * @return mixed
17
     */
18
    public function handle(Request $request, Closure $next, $roles)
19
    {
20
        $hasAccess = false;
21
        $roles_array = explode('|', $roles);
22
        foreach ($roles_array as $role) {
23
            $hasAccess = $hasAccess || Auth::user()->hasRole($role);
0 ignored issues
show
Bug introduced by
The method hasRole() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

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

23
            $hasAccess = $hasAccess || Auth::user()->/** @scrutinizer ignore-call */ hasRole($role);
Loading history...
24
        }
25
26
        return $hasAccess ? $next($request) : redirect()->back()->withFail('Sorry you don\'t have access.');
27
    }
28
}
29