NewPasswordController::store()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 30
ccs 20
cts 20
cp 1
crap 2
rs 9.7333
1
<?php
2
3
namespace App\Http\Controllers\Admin\Auth;
4
5
use App\Http\Controllers\Controller;
6
use Illuminate\Auth\Events\PasswordReset;
7
use Illuminate\Http\RedirectResponse;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Hash;
10
use Illuminate\Support\Facades\Password;
11
use Illuminate\Support\Str;
12
use Illuminate\Validation\Rules;
13
use Illuminate\View\View;
14
15
class NewPasswordController extends Controller
16
{
17
    /**
18
     * Display the password reset view.
19
     */
20 1
    public function create(Request $request): View
21
    {
22 1
        return view('admin.auth.reset-password', ['request' => $request]);
23
    }
24
25
    /**
26
     * Handle an incoming new password request.
27
     *
28
     * @throws \Illuminate\Validation\ValidationException
29
     */
30 1
    public function store(Request $request): RedirectResponse
31
    {
32 1
        $request->validate([
33 1
            'token' => ['required'],
34 1
            'email' => ['required', 'email'],
35 1
            'password' => ['required', 'confirmed', Rules\Password::defaults()],
36 1
        ]);
37
38
        // Here we will attempt to reset the admin's password. If it is successful we
39
        // will update the password on an actual user model and persist it to the
40
        // database. Otherwise we will parse the error and return the response.
41 1
        $status = Password::broker('admins')->reset(
42 1
            $request->only('email', 'password', 'password_confirmation', 'token'),
43 1
            function ($admin) use ($request) {
44 1
                $admin->forceFill([
45 1
                    'password' => Hash::make($request->password),
46 1
                    'remember_token' => Str::random(60),
47 1
                ])->save();
48
49 1
                event(new PasswordReset($admin));
50 1
            }
51 1
        );
52
53
        // If the password was successfully reset, we will redirect the user back to
54
        // the application's home authenticated view. If there is an error we can
55
        // redirect them back to where they came from with their error message.
56 1
        return $status == Password::PASSWORD_RESET
57 1
                    ? redirect()->route('admin.login')->with('status', __($status))
58 1
                    : back()->withInput($request->only('email'))
59 1
                            ->withErrors(['email' => __($status)]);
60
    }
61
}
62