Passed
Pull Request — dev (#1123)
by
unknown
06:52
created

ForgotPasswordController::showLinkRequestForm()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 57
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 35
nc 9
nop 1
dl 0
loc 57
rs 8.1155
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 72

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\SendPasswordForgottenEmail;
7
use App\Models\Settings;
8
use App\Models\User;
9
use DariusIII\Token\Facades\Token;
10
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
11
use Illuminate\Http\Request;
12
13
class ForgotPasswordController extends Controller
14
{
15
    /*
16
    |--------------------------------------------------------------------------
17
    | Password Reset Controller
18
    |--------------------------------------------------------------------------
19
    |
20
    | This controller is responsible for handling password reset emails and
21
    | includes a trait which assists in sending these notifications from
22
    | your application to your users. Feel free to explore this trait.
23
    |
24
    */
25
26
    use SendsPasswordResetEmails;
27
28
    /**
29
     * Create a new controller instance.
30
     *
31
     * @return void
32
     */
33
    public function __construct()
34
    {
35
        $this->middleware('guest');
36
    }
37
38
    /**
39
     * @param \Illuminate\Http\Request $request
40
     *
41
     * @throws \Exception
42
     */
43
    public function showLinkRequestForm(Request $request)
44
    {
45
        $sent = '';
46
        $email = request()->input('email') ?? '';
47
        $rssToken = request()->input('apikey') ?? '';
48
        if (empty($email) && empty($rssToken)) {
49
            app('smarty.view')->assign('error', 'Missing parameter(email and/or apikey to send password reset');
50
        } else {
51
            if (config('captcha.enabled') === true && (! empty(config('captcha.secret')) && ! empty(config('captcha.sitekey')))) {
52
                $this->validate($request, [
53
                    'g-recaptcha-response' => 'required|captcha',
54
                ]);
55
            }
56
            //
57
            // Check users exists and send an email
58
            //
59
            $ret = ! empty($rssToken) ? User::getByRssToken($rssToken) : User::getByEmail($email);
60
            if ($ret === null) {
61
                app('smarty.view')->assign('error', 'The email or apikey are not recognised.');
62
                $sent = true;
63
            } else {
64
                //
65
                // Generate a forgottenpassword guid, store it in the user table
66
                //
67
                $guid = /Token::random(32);
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected '/' on line 67 at column 24
Loading history...
68
                User::updatePassResetGuid($ret['id'], $guid);
69
                //
70
                // Send the email
71
                //
72
                $resetLink = url('/').'/resetpassword?guid='.$guid;
73
                SendPasswordForgottenEmail::dispatch($ret, $resetLink);
74
                $sent = true;
75
            }
76
        }
77
78
        $theme = Settings::settingValue('site.main.style');
79
80
        $title = 'Forgotten Password';
81
        $meta_title = 'Forgotten Password';
82
        $meta_keywords = 'forgotten,password,signup,registration';
83
        $meta_description = 'Forgotten Password';
84
85
        $content = app('smarty.view')->fetch($theme.'/forgottenpassword.tpl');
86
87
        app('smarty.view')->assign(
88
            [
89
                'content' => $content,
90
                'title' => $title,
91
                'meta_title' => $meta_title,
92
                'meta_keywords' => $meta_keywords,
93
                'meta_description' => $meta_description,
94
                'email'     => $email,
95
                'apikey'    => $rssToken,
96
                'sent'      => $sent,
97
            ]
98
        );
99
        app('smarty.view')->display($theme.'/basepage.tpl');
100
    }
101
}
102