RedirectIfNotAdmin   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 37.5%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 8
c 2
b 0
f 0
dl 0
loc 19
ccs 3
cts 8
cp 0.375
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 12 3
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Auth\AuthenticationException;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Auth;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * @see \App\Http\Middleware\Authenticate
13
 */
14
class RedirectIfNotAdmin
15
{
16
    /**
17
     * Handle an incoming request.
18
     *
19
     * @throws \Illuminate\Auth\AuthenticationException
20
     */
21 15
    public function handle(Request $request, Closure $next, string $guard = 'admin'): Response
22
    {
23 15
        if (Auth::guard($guard)->check()) {
24 15
            return $next($request);
25
        }
26
27
        $redirectToRoute = $request->expectsJson() ? '' : route('admin.login');
28
29
        throw new AuthenticationException(
30
            'Unauthenticated.',
31
            [$guard],
32
            $redirectToRoute
33
        );
34
    }
35
}
36