ResetPasswordController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 86
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sendResetLinkEmail() 0 18 2
A resetPassword() 0 6 1
A reset() 0 23 2
A broker() 0 3 1
A credentials() 0 7 1
1
<?php
2
3
namespace Signifly\Janitor\Http\Controllers;
4
5
use Illuminate\Http\JsonResponse;
6
use Illuminate\Http\Request;
7
use Illuminate\Routing\Controller;
8
use Illuminate\Support\Facades\Password;
9
use Illuminate\Support\Str;
10
11
class ResetPasswordController extends Controller
12
{
13
    public function sendResetLinkEmail(Request $request)
14
    {
15
        $request->validate([
16
            'email' => 'required|email',
17
        ]);
18
19
        // We will send the password reset link to this user. Once we have attempted
20
        // to send the link, we will examine the response then see the message we
21
        // need to show to the user. Finally, we'll send out a proper response.
22
        $response = $this->broker()->sendResetLink(
23
            $request->only('email')
24
        );
25
26
        if ($response == Password::RESET_LINK_SENT) {
27
            return new JsonResponse(['message' => trans('passwords.sent')]);
28
        }
29
30
        return new JsonResponse(['message' => trans($response)], 422);
31
    }
32
33
    public function reset(Request $request)
34
    {
35
        $request->validate([
36
            'token' => 'required',
37
            'email' => 'required|email',
38
            'password' => 'required|confirmed|min:6',
39
        ]);
40
41
        // Here we will attempt to reset the user's password. If it is successful we
42
        // will update the password on an actual user model and persist it to the
43
        // database. Otherwise we will parse the error and return the response.
44
        $response = $this->broker()->reset(
45
            $this->credentials($request),
46
            function ($user, $password) {
47
                $this->resetPassword($user, $password);
48
            }
49
        );
50
51
        if ($response == Password::PASSWORD_RESET) {
52
            return new JsonResponse(['message' => trans($response)]);
53
        }
54
55
        return new JsonResponse(['message' => trans($response)], 422);
56
    }
57
58
    /**
59
     * Get the password reset credentials from the request.
60
     *
61
     * @param  \Illuminate\Http\Request  $request
62
     * @return array
63
     */
64
    protected function credentials(Request $request)
65
    {
66
        return $request->only(
67
            'email',
68
            'password',
69
            'password_confirmation',
70
            'token'
71
        );
72
    }
73
74
    /**
75
     * Reset the given user's password.
76
     *
77
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
78
     * @param  string  $password
79
     * @return void
80
     */
81
    protected function resetPassword($user, $password)
82
    {
83
        $user->forceFill([
84
            'password' => bcrypt($password),
85
            'remember_token' => Str::random(60),
86
        ])->save();
87
    }
88
89
    /**
90
     * Get the broker to be used during password reset.
91
     *
92
     * @return \Illuminate\Contracts\Auth\PasswordBroker
93
     */
94
    protected function broker()
95
    {
96
        return Password::broker();
97
    }
98
}
99