ResetPasswordController::sendResetResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Http\Controllers\Controller;
6
use Illuminate\Foundation\Auth\ResetsPasswords;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Password;
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;
25
26
    /**
27
     * Reset the given user's password.
28
     *
29
     * @param \Illuminate\Http\Request $request
30
     *
31
     * @return \Illuminate\Http\RedirectResponse
32
     */
33
    public function reset(Request $request)
34
    {
35
        $this->validate($request, $this->rules(), $this->validationErrorMessages());
36
37
        // Here we will attempt to reset the user's password. If it is successful we
38
        // will update the password on an actual user model and persist it to the
39
        // database. Otherwise we will parse the error and return the response.
40
        $response = $this->broker()->reset(
41
            $this->credentials($request), function ($user, $password) {
42
                $this->resetPassword($user, $password);
43
            }
44
        );
45
46
        // If the password was successfully reset, we will redirect the user back to
47
        // the application's home authenticated view. If there is an error we can
48
        // redirect them back to where they came from with their error message.
49
        return $response == Password::PASSWORD_RESET
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response == Illu...se($request, $response) also could return the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\RedirectResponse.
Loading history...
50
            ? $this->sendResetResponse($request, $response)
51
            : $this->sendResetFailedResponse($request, $response);
52
    }
53
54
    /**
55
     * Get the response for a successful password reset.
56
     *
57
     * @param  \Illuminate\Http\Request
58
     * @param string $response
59
     *
60
     * @return \Illuminate\Http\RedirectResponse
61
     */
62
    protected function sendResetResponse(Request $request, $response)
63
    {
64
        if ($request->expectsJson()) {
65
            return response()->json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...' => trans($response))) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\RedirectResponse.
Loading history...
66
                'status' => trans($response),
67
            ]);
68
        }
69
70
        return redirect($this->redirectPath())
71
            ->with('status', trans($response));
72
    }
73
74
    /**
75
     * Get the response for a failed password reset.
76
     *
77
     * @param  \Illuminate\Http\Request
78
     * @param string $response
79
     *
80
     * @return mixed
81
     */
82
    protected function sendResetFailedResponse(Request $request, $response)
83
    {
84
        if ($request->expectsJson()) {
85
            return new JsonResponse(['email' => trans($response)], 422);
86
        }
87
88
        return redirect()->back()
89
            ->withInput($request->only('email'))
90
            ->withErrors(['email' => trans($response)]);
91
    }
92
93
    /**
94
     * Display the password reset view for the given token.
95
     *
96
     * If no token is present, display the link request form.
97
     *
98
     * @param \Illuminate\Http\Request $request
99
     * @param string|null              $token
100
     *
101
     * @return \Illuminate\Http\Response
102
     */
103
    public function showResetForm(Request $request, $token = null)
104
    {
105
        return view('adminlte::auth.passwords.reset')->with(
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('adminlte::a...l' => $request->email)) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
106
            ['token' => $token, 'email' => $request->email]
107
        );
108
    }
109
110
    /**
111
     * Create a new controller instance.
112
     *
113
     * @return void
114
     */
115
    public function __construct()
116
    {
117
        $this->middleware('guest');
118
    }
119
}
120