Completed
Push — master ( 55e392...61eb83 )
by Ryan
02:30
created

Authenticate::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
1
<?php namespace Anomaly\UsersModule\Http\Middleware;
2
3
use Anomaly\UsersModule\User\UserSecurity;
4
use Closure;
5
use Illuminate\Contracts\Auth\Guard;
6
use Illuminate\Http\Request;
7
use Illuminate\Routing\Redirector;
8
use Illuminate\Routing\ResponseFactory;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * Class AuthenticateRequest
13
 *
14
 * This class replaces the Laravel version in app/
15
 *
16
 * @link          http://pyrocms.com/
17
 * @author        PyroCMS, Inc. <[email protected]>
18
 * @author        Ryan Thompson <[email protected]>
19
 * @package       Anomaly\UsersModule\Http\Middleware
20
 */
21
class Authenticate
22
{
23
24
    /**
25
     * The Guard implementation.
26
     *
27
     * @var Guard
28
     */
29
    protected $guard;
30
31
    /**
32
     * The security utility.
33
     *
34
     * @var UserSecurity
35
     */
36
    protected $security;
37
38
    /**
39
     * The response factory.
40
     *
41
     * @var ResponseFactory
42
     */
43
    protected $response;
44
45
    /**
46
     * The redirector utility.
47
     *
48
     * @var Redirector
49
     */
50
    protected $redirect;
51
52
    /**
53
     * Create a new AuthenticateRequest instance.
54
     *
55
     * @param Guard           $guard
56
     * @param Redirector      $redirect
57
     * @param ResponseFactory $response
58
     * @param UserSecurity    $security
59
     */
60 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
61
        Guard $guard,
62
        Redirector $redirect,
63
        ResponseFactory $response,
64
        UserSecurity $security
65
    ) {
66
        $this->guard    = $guard;
67
        $this->redirect = $redirect;
68
        $this->response = $response;
69
        $this->security = $security;
70
    }
71
72
    /**
73
     * Handle an incoming request.
74
     *
75
     * @param  \Illuminate\Http\Request $request
76
     * @param  \Closure                 $next
77
     * @return mixed
78
     */
79
    public function handle(Request $request, Closure $next)
80
    {
81
        if ($this->guard->guest()) {
82
            if ($request->ajax()) {
83
                return $this->response->make('Unauthorized.', 401);
84
            } else {
85
                if ($request->segment(1) === 'admin') {
86
                    return $this->redirect->guest('admin/login');
87
                } else {
88
                    return $this->redirect->guest('login');
89
                }
90
            }
91
        }
92
93
        return $next($request);
94
    }
95
}
96