AuthController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 6
dl 0
loc 91
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A login() 0 4 1
A register() 0 4 1
A logout() 0 7 1
A handleLogin() 0 21 2
A handleRegister() 0 23 2
1
<?php
2
3
namespace NukaCode\Users\Http\Controllers;
4
5
use App\Http\Controllers\BaseController;
6
use App\Models\User;
7
use Illuminate\Support\Facades\DB;
8
use NukaCode\Users\Events\UserLoggedIn;
9
use NukaCode\Users\Events\UserRegistered;
10
use NukaCode\Users\Http\Requests\Login;
11
use NukaCode\Users\Http\Requests\Registration;
12
13
class AuthController extends BaseController
14
{
15
    /**
16
     * Display the login form.
17
     */
18
    public function login()
19
    {
20
        //
21
    }
22
23
    /**
24
     * Log the user in
25
     *
26
     * @param Login $request
27
     *
28
     * @return mixed
29
     */
30
    public function handleLogin(Login $request)
31
    {
32
        // Set the auth data
33
        $userData = [
34
            'username' => $request->get('username'),
35
            'password' => $request->get('password'),
36
        ];
37
38
        // Log in successful
39
        if (auth()->attempt($userData, $request->get('remember', false))) {
40
            event(new UserLoggedIn(auth()->user()));
0 ignored issues
show
Documentation introduced by
new \NukaCode\Users\Even...oggedIn(auth()->user()) is of type object<NukaCode\Users\Events\UserLoggedIn>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
42
            return redirect()
43
                ->intended(route('home'))
44
                ->with('message', 'You have been logged in.');
45
        }
46
47
        // Login failed
48
        return redirect(route('auth.login'))
49
            ->with('errors', ['Your username or password was incorrect.']);
50
    }
51
52
    /**
53
     * Display the registration form.
54
     */
55
    public function register()
56
    {
57
        //
58
    }
59
60
    /**
61
     * Register a user
62
     *
63
     * @param Registration $request
64
     *
65
     * @return mixed
66
     */
67
    public function handleRegister(Registration $request)
68
    {
69
        DB::beginTransaction();
70
71
        try {
72
            $user = User::create($request->all());
73
            $user->assignRole(config('users.default'));
74
75
            auth()->login($user);
76
77
            event(new UserRegistered(auth()->user()));
0 ignored issues
show
Documentation introduced by
new \NukaCode\Users\Even...istered(auth()->user()) is of type object<NukaCode\Users\Events\UserRegistered>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        } catch (\Exception $exception) {
79
            DB::rollBack();
80
81
            return redirect(route('auth.register'))
82
                ->with('errors', $exception->getMessage());
83
        }
84
85
        DB::commit();
86
87
        return redirect(route('home'))
88
            ->with('message', 'Your account has been created.');
89
    }
90
91
    /**
92
     * Log the user out.
93
     *
94
     * @return mixed
95
     */
96
    public function logout()
97
    {
98
        auth()->logout();
99
100
        return redirect(route('home'))
101
            ->with('message', 'You have been logged out.');
102
    }
103
}
104