Completed
Push — master ( 77edab...304033 )
by Chris
11s
created

ForgotPasswordController::sendResetEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Yab\FlightDeck\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\Response;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Routing\Controller;
9
use Illuminate\Support\Facades\Password;
10
use Yab\FlightDeck\Http\Requests\ForgotPasswordRequest;
11
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
12
13
class ForgotPasswordController extends Controller
14
{
15
    /*
16
    |--------------------------------------------------------------------------
17
    | Password Reset Controller
18
    |--------------------------------------------------------------------------
19
    |
20
    | This controller is responsible for handling password reset emails and
21
    | includes a trait which assists in sending these notifications from
22
    | your application to your users. Feel free to explore this trait.
23
    |
24
    */
25
26
    use SendsPasswordResetEmails;
27
28
    /**
29
     * Create a new controller instance.
30
     *
31
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
32
     */
33
    public function __construct()
34
    {
35
        $this->middleware('guest');
36
    }
37
38
    /**
39
     * Send a reset link to the given user.
40
     *
41
     * @param  Yab\FlightDeck\Http\Requests\ForgotPasswordRequest $request
42
     * @return \Illuminate\Http\JsonResponse
43
     */
44
    public function sendResetEmail(ForgotPasswordRequest $request)
45
    {
46
        $response = $this->broker()->sendResetLink(
47
            $request->only('email')
48
        );
49
50
        return $response == Password::RESET_LINK_SENT
51
            ? $this->sendResetLinkResponse($request, $response)
52
            : $this->sendResetLinkFailedResponse($request, $response);
53
    }
54
    
55
    /**
56
     * Get the response for a successful password reset link.
57
     *
58
     * @param  \Illuminate\Http\Request  $request
59
     * @param  string  $response
60
     * @return \Illuminate\Http\JsonResponse
61
     */
62
    protected function sendResetLinkResponse(Request $request, $response)
63
    {
64
        return new JsonResponse([
65
            'success' => true,
66
            'message' => '',
67
        ], Response::HTTP_OK);
68
    }
69
70
    /**
71
     * Get the response for a failed password reset link.
72
     *
73
     * @param  \Illuminate\Http\Request  $request
74
     * @param  string  $response
75
     * @return \Illuminate\Http\JsonResponse
76
     */
77
    protected function sendResetLinkFailedResponse(Request $request, $response)
78
    {
79
        return new JsonResponse([
80
            'success' => false,
81
            'message' => 'An error occurred while trying to send the password reset link',
82
        ], Response::HTTP_INTERNAL_SERVER_ERROR);
83
    }
84
}
85