AdminOnly::handle()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 3
dl 0
loc 13
ccs 5
cts 6
cp 0.8333
crap 4.074
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Support\Facades\Auth;
7
8
class AdminOnly
9
{
10
    /**
11
     * Handle an incoming request.
12
     *
13
     * @param  \Illuminate\Http\Request $request
14
     * @param  \Closure $next
15
     * @return mixed
16
     */
17 3
    public function handle($request, Closure $next, $guard = null)
18
    {
19 3
        if (!Auth::guard($guard)->user()->isAdmin()) {
20 1
            if ($request->ajax() || $request->wantsJson()) {
21
                return response(trans('general.text.unauthorized'), 401);
0 ignored issues
show
Bug introduced by
It seems like trans('general.text.unauthorized') targeting trans() can also be of type object<Illuminate\Contra...Translation\Translator>; however, response() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
22
            } else {
23
                //FIXME: Errors not working
24 1
                return redirect('/')->withErrors(['msg', trans('general.text.unauthorized')]);
25
            }
26
        }
27
28 2
        return $next($request);
29
    }
30
}
31