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

Authenticate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 14.67 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
c 2
b 1
f 0
lcom 1
cbo 0
dl 11
loc 75
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
A handle() 0 16 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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