Completed
Pull Request — master (#45)
by Şəhriyar
11:38
created

Authenticate::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 4
cts 6
cp 0.6667
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
crap 3.3332
1
<?php namespace App\Http\Middleware;
2
3
use Illuminate\Contracts\Auth\Guard;
4
use Illuminate\Http\Request;
5
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
6
7
class Authenticate
8
{
9
    /**
10
     * The Guard implementation.
11
     *
12
     * @var Guard
13
     */
14
    protected $auth;
15
16
    /**
17
     * Create a new filter instance.
18
     *
19
     * @param  Guard $auth
20
     */
21 1
    public function __construct(Guard $auth)
22
    {
23 1
        $this->auth = $auth;
24 1
    }
25
26
    /**
27
     * Handle an incoming request.
28
     *
29
     * @param  Request  $request
30
     * @param  \Closure $next
31
     *
32
     * @return mixed
33
     */
34 1
    public function handle(Request $request, \Closure $next)
35
    {
36 1
        if (app('auth.driver')->guest()) {
37 1
            if ($request->expectsJson()) {
38
                throw new UnauthorizedHttpException('Unauthorized');
39
            } else {
40 1
                return redirect()->guest(route(app('translator')->getLocale() . '.login'));
41
            }
42
        }
43
44
        return $next($request);
45
    }
46
}
47