Passed
Push — master ( 8f3742...50f570 )
by Darko
08:41
created

ResetPasswordController::reset()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 56
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 37
c 1
b 1
f 0
dl 0
loc 56
rs 8.7057
cc 6
nc 8
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Http\Controllers\Controller;
6
use App\Jobs\SendPasswordResetEmail;
7
use App\Models\User;
8
use Illuminate\Foundation\Auth\ResetsPasswords;
9
use Illuminate\Http\Request;
10
11
class ResetPasswordController extends Controller
12
{
13
    /*
14
    |--------------------------------------------------------------------------
15
    | Password Reset Controller
16
    |--------------------------------------------------------------------------
17
    |
18
    | This controller is responsible for handling password reset requests
19
    | and uses a simple trait to include this behavior. You're free to
20
    | explore this trait and override any methods you wish to tweak.
21
    |
22
    */
23
24
    use ResetsPasswords;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Foundation\Auth\ResetsPasswords requires the property $email which is not provided by App\Http\Controllers\Auth\ResetPasswordController.
Loading history...
25
26
    /**
27
     * Where to redirect users after resetting their password.
28
     */
29
    protected string $redirectTo = '/';
30
31
    /**
32
     * Create a new controller instance.
33
     *
34
     * @return void
35
     */
36
    public function __construct()
37
    {
38
        $this->middleware('guest');
39
    }
40
41
    /**
42
     * @throws \Exception
43
     */
44
    public function reset(Request $request): void
45
    {
46
        $error = '';
47
        $confirmed = '';
48
        $onscreen = '';
49
        if ($request->missing('guid')) {
50
            $error = 'No reset code provided.';
51
        }
52
53
        if ($error === '') {
54
            $ret = User::getByPassResetGuid($request->input('guid'));
55
            if ($ret === null) {
56
                $error = 'Bad reset code provided.';
57
            } else {
58
                // Check if user is soft deleted
59
                $user = User::withTrashed()->find($ret['id']);
60
                if ($user && $user->trashed()) {
61
                    $error = 'This account has been deactivated.';
62
                } else {
63
                    //
64
                    // reset the password, inform the user, send out the email
65
                    //
66
                    User::updatePassResetGuid($ret['id'], '');
67
                    $newpass = User::generatePassword();
68
                    User::updatePassword($ret['id'], $newpass);
69
70
                    $onscreen = 'Your password has been reset to <strong>'.$newpass.'</strong> and sent to your e-mail address.';
71
                    SendPasswordResetEmail::dispatch($ret, $newpass);
72
                    $confirmed = true;
73
                }
74
            }
75
        }
76
77
        $theme = 'Gentele';
78
79
        $title = 'Forgotten Password';
80
        $meta_title = 'Forgotten Password';
81
        $meta_keywords = 'forgotten,password,signup,registration';
82
        $meta_description = 'Forgotten Password';
83
84
        $content = app('smarty.view')->fetch($theme.'/forgottenpassword.tpl');
85
86
        app('smarty.view')->assign(
87
            [
88
                'content' => $content,
89
                'title' => $title,
90
                'meta_title' => $meta_title,
91
                'meta_keywords' => $meta_keywords,
92
                'meta_description' => $meta_description,
93
                'email' => $ret['email'] ?? '',
94
                'confirmed' => $confirmed,
95
                'error' => $error,
96
                'notice' => $onscreen,
97
            ]
98
        );
99
        app('smarty.view')->display($theme.'/basepage.tpl');
100
    }
101
}
102