Completed
Push — master ( f3c73f...660e59 )
by Glenn
03:41 queued 01:27
created

AuthController::authenticated()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\User;
6
use Validator;
7
use App\Http\Controllers\Controller;
8
use Illuminate\Foundation\Auth\ThrottlesLogins;
9
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
10
use Bouncer;
11
12
class AuthController extends Controller
13
{
14
    /*
15
    |--------------------------------------------------------------------------
16
    | Registration & Login Controller
17
    |--------------------------------------------------------------------------
18
    |
19
    | This controller handles the registration of new users, as well as the
20
    | authentication of existing users. By default, this controller uses
21
    | a simple trait to add these behaviors. Why don't you explore it?
22
    |
23
    */
24
25
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
26
27
    /**
28
     * Where to redirect users after login / registration.
29
     *
30
     * @var string
31
     */
32
    protected $redirectTo = '/';
33
34
    /**
35
     * Create a new authentication controller instance.
36
     *
37
     * @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...
38
     */
39
    public function __construct()
40
    {
41
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
42
        $this->middleware('lang');
43
44
    }
45
46
47 View Code Duplication
    protected function authenticated()
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...
48
    {
49
      $user = auth()->user();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
50
51
            if(Bouncer::is($user)->a('Administrator', 'Manager')) {
52
                return redirect()->route('dashboard.administration');
53
            }
54
55
            elseif(Bouncer::is($user)->an('Agents')) {
56
              return redirect()->route('dashboard.agent');
57
            }
58
59
            elseif(Bouncer::is($user)->an('Customer')) {
60
                return redirect('/');
61
            }
62
63
            return redirect('/');
64
     }
65
66
    /**
67
     * Get a validator for an incoming registration request.
68
     *
69
     * @param  array  $data
70
     * @return \Illuminate\Contracts\Validation\Validator
71
     */
72
    protected function validator(array $data)
73
    {
74
        return Validator::make($data, [
75
            'fname' => 'required|max:255',
76
            'name' => 'required|max:255',
77
            'email' => 'required|email|max:255|unique:users',
78
            'password' => 'required|min:6|confirmed',
79
        ]);
80
    }
81
82
    /**
83
     * Create a new user instance after a valid registration.
84
     *
85
     * @param  array  $data
86
     * @return User
87
     */
88
    protected function create(array $data)
89
    {
90
        $newUser = User::create([
91
            'fname' => $data['fname'],
92
            'name' => $data['name'],
93
            'email' => $data['email'],
94
            'password' => bcrypt($data['password']),
95
        ]);
96
97
        Bouncer::assign('Guest')->to($newUser);
98
99
        return $newUser;
100
    }
101
}
102