ResetsPasswords::credentials()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace MedianetDev\LaravelAuthApi\Http\Controllers\Traits;
4
5
use Illuminate\Auth\Events\PasswordReset;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Hash;
9
use Illuminate\Support\Facades\Password;
10
use Illuminate\Support\Str;
11
use MedianetDev\LaravelAuthApi\Http\Helpers\ApiResponse;
12
13
trait ResetsPasswords
14
{
15
    /**
16
     * Reset the given user's password.
17
     *
18
     * @param \Illuminate\Http\Request $request
19
     *
20
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
21
     */
22
    public function reset(Request $request)
23
    {
24
        $request->validate($this->rules(), $this->validationErrorMessages());
25
        // Here we will attempt to reset the user's password. If it is successful we
26
        // will update the password on an actual user model and persist it to the
27
        // database. Otherwise we will parse the error and return the response.
28
        $response = $this->broker()->reset(
29
            $this->credentials($request), function ($user, $password) {
30
                $this->resetPassword($user, $password);
31
            }
32
        );
33
34
        // If the password was successfully reset, we will redirect the user back to
35
        // the application's home authenticated view. If there is an error we can
36
        // redirect them back to where they came from with their error message.
37
        return Password::PASSWORD_RESET == $response
38
                    ? $this->sendResetResponse($request, $response)
39
                    : $this->sendResetFailedResponse($request, $response);
40
    }
41
42
    /**
43
     * Get the password reset validation rules.
44
     *
45
     * @return array
46
     */
47
    protected function rules()
48
    {
49
        return [
50
            'token' => 'required',
51
            'email' => 'required|email',
52
            'password' => 'required|min:8',
53
            'confirmation_password' => 'required|same:password|min:8',
54
        ];
55
    }
56
57
    /**
58
     * Get the password reset validation error messages.
59
     *
60
     * @return array
61
     */
62
    protected function validationErrorMessages()
63
    {
64
        return [];
65
    }
66
67
    /**
68
     * Get the password reset credentials from the request.
69
     *
70
     * @param \Illuminate\Http\Request $request
71
     *
72
     * @return array
73
     */
74
    protected function credentials(Request $request)
75
    {
76
        return $request->only(
77
            'email', 'password', 'password_confirmation', 'token'
78
        );
79
    }
80
81
    /**
82
     * Reset the given user's password.
83
     *
84
     * @param \Illuminate\Contracts\Auth\CanResetPassword $user
85
     * @param string                                      $password
86
     *
87
     * @return void
88
     */
89
    protected function resetPassword($user, $password)
90
    {
91
        $user->password = Hash::make($password);
0 ignored issues
show
Bug introduced by
Accessing password on the interface Illuminate\Contracts\Auth\CanResetPassword suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
92
93
        $user->setRememberToken(Str::random(60));
94
95
        $user->save();
96
97
        event(new PasswordReset($user));
98
99
        $this->guard()->login($user);
100
    }
101
102
    /**
103
     * Get the response for a successful password reset.
104
     *
105
     * @param \Illuminate\Http\Request $request
106
     * @param string                   $response
107
     *
108
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
109
     */
110
    protected function sendResetResponse(Request $request, $response)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

110
    protected function sendResetResponse(/** @scrutinizer ignore-unused */ Request $request, $response)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
    {
112
        return ApiResponse::send(['status' => trans($response)], 1, 200, trans($response));
0 ignored issues
show
Bug Best Practice introduced by
The expression return MedianetDev\Larav... 200, trans($response)) returns the type Illuminate\Http\Response which is incompatible with the documented return type Illuminate\Http\JsonResp...e\Http\RedirectResponse.
Loading history...
Bug introduced by
It seems like trans($response) can also be of type array and array; however, parameter $message of MedianetDev\LaravelAuthA...ers\ApiResponse::send() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
        return ApiResponse::send(['status' => trans($response)], 1, 200, /** @scrutinizer ignore-type */ trans($response));
Loading history...
113
    }
114
115
    /**
116
     * Get the response for a failed password reset.
117
     *
118
     * @param \Illuminate\Http\Request $request
119
     * @param string                   $response
120
     *
121
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
122
     */
123
    protected function sendResetFailedResponse(Request $request, $response)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

123
    protected function sendResetFailedResponse(/** @scrutinizer ignore-unused */ Request $request, $response)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
124
    {
125
        return ApiResponse::send(['status' => trans($response)], 0, 200, trans($response));
0 ignored issues
show
Bug introduced by
It seems like trans($response) can also be of type array and array; however, parameter $message of MedianetDev\LaravelAuthA...ers\ApiResponse::send() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
        return ApiResponse::send(['status' => trans($response)], 0, 200, /** @scrutinizer ignore-type */ trans($response));
Loading history...
Bug Best Practice introduced by
The expression return MedianetDev\Larav... 200, trans($response)) returns the type Illuminate\Http\Response which is incompatible with the documented return type Illuminate\Http\JsonResp...e\Http\RedirectResponse.
Loading history...
126
    }
127
128
    /**
129
     * Get the broker to be used during password reset.
130
     *
131
     * @return \Illuminate\Contracts\Auth\PasswordBroker
132
     */
133
    public function broker()
134
    {
135
        return Password::broker();
136
    }
137
138
    /**
139
     * Get the guard to be used during password reset.
140
     *
141
     * @return \Illuminate\Contracts\Auth\StatefulGuard
142
     */
143
    protected function guard()
144
    {
145
        return Auth::guard();
146
    }
147
}
148