SendsPasswordResetEmails::generateToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 10
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\Http\Request;
6
use Illuminate\Support\Facades\Password;
7
use MedianetDev\LaravelAuthApi\Http\Helpers\ApiResponse;
8
use MedianetDev\LaravelAuthApi\Models\ApiUser;
9
10
trait SendsPasswordResetEmails
11
{
12
    /**
13
     * Send a reset link to the given user.
14
     *
15
     * @param \Illuminate\Http\Request $request
16
     *
17
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
18
     */
19
    public function sendResetLinkEmail(Request $request)
20
    {
21
        $this->validateEmail($request);
22
        // We will send the password reset link to this user. Once we have attempted
23
        // to send the link, we will examine the response then see the message we
24
        // need to show to the user. Finally, we'll send out a proper response.
25
26
        // find the user
27
        $user = ApiUser::where('email', $this->credentials($request))->first();
28
29
        // sent user not found response
30
        if (! $user) {
31
            return $this->sendResetLinkFailedResponse($request, Password::INVALID_USER);
32
        }
33
34
        // generate the token
35
        $token = $this->generateToken($user);
36
37
        // send the mail
38
        $user->sendPasswordResetNotification($token);
39
40
        // send email sent successfully response
41
        return $this->sendResetLinkResponse($request, Password::RESET_LINK_SENT);
42
    }
43
44
    private function generateToken($user)
45
    {
46
        $token = mt_rand(1000, 9999);
47
        \DB::table('password_resets')->where('email', $user->email)->delete();
48
        \DB::table('password_resets')->insert([
49
            'email' => $user->email,
50
            'token' => \Hash::make($token), //change 60 to any length you want
51
            'created_at' => \Carbon\Carbon::now(),
52
        ]);
53
54
        return $token;
55
    }
56
57
    /**
58
     * Validate the email for the given request.
59
     *
60
     * @param \Illuminate\Http\Request $request
61
     *
62
     * @return void
63
     */
64
    protected function validateEmail(Request $request)
65
    {
66
        $request->validate(['email' => 'required|email']);
67
    }
68
69
    /**
70
     * Get the needed authentication credentials from the request.
71
     *
72
     * @param \Illuminate\Http\Request $request
73
     *
74
     * @return array
75
     */
76
    protected function credentials(Request $request)
77
    {
78
        return $request->only('email');
79
    }
80
81
    /**
82
     * Get the response for a successful password reset link.
83
     *
84
     * @param \Illuminate\Http\Request $request
85
     * @param string                   $response
86
     *
87
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
88
     */
89
    protected function sendResetLinkResponse(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

89
    protected function sendResetLinkResponse(/** @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...
90
    {
91
        return ApiResponse::send(['status' => trans($response)], 1, 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

91
        return ApiResponse::send(['status' => trans($response)], 1, 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...
92
    }
93
94
    /**
95
     * Get the response for a failed password reset link.
96
     *
97
     * @param \Illuminate\Http\Request $request
98
     * @param string                   $response
99
     *
100
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
101
     */
102
    protected function sendResetLinkFailedResponse(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

102
    protected function sendResetLinkFailedResponse(/** @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...
103
    {
104
        return ApiResponse::send(['status' => trans($response)], 0, 422, trans($response));
0 ignored issues
show
Bug Best Practice introduced by
The expression return MedianetDev\Larav... 422, 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

104
        return ApiResponse::send(['status' => trans($response)], 0, 422, /** @scrutinizer ignore-type */ trans($response));
Loading history...
105
    }
106
}
107