Authenticate::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 7
Ratio 50 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 7
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Contracts\Auth\Guard;
7
8
/**
9
 * Authenticate.
10
 *
11
 * @author    Ladybird <[email protected]>
12
 */
13
class Authenticate
14
{
15
    /**
16
     * The Guard implementation.
17
     *
18
     * @var Guard
19
     */
20
    protected $auth;
21
22
    /**
23
     * Create a new filter instance.
24
     *
25
     * @param Guard $auth
26
     *
27
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
28
     */
29
    public function __construct(Guard $auth)
30
    {
31
        $this->auth = $auth;
32
    }
33
34
    /**
35
     * Handle an incoming request.
36
     *
37
     * @param \Illuminate\Http\Request $request
38
     * @param \Closure                 $next
39
     *
40
     * @return mixed
41
     */
42
    public function handle($request, Closure $next)
43
    {
44
        if ($this->auth->guest()) {
45 View Code Duplication
            if ($request->ajax()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
                $result = ['fails' => 'Unauthorized! Please login again'];
47
48
                return response()->json(compact('result'));
49
            } else {
50
                return redirect()->guest('auth/login');
51
            }
52
        }
53
54
        return $next($request);
55
    }
56
}
57