Completed
Push — master ( ad8d2b...c8618f )
by Manel
55:15 queued 25:14
created

ForgotPasswordController::sendResetLinkResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Http\Controllers\Controller;
6
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Password;
10
11
class ForgotPasswordController extends Controller
12
{
13
    /*
14
    |--------------------------------------------------------------------------
15
    | Password Reset Controller
16
    |--------------------------------------------------------------------------
17
    |
18
    | This controller is responsible for handling password reset emails and
19
    | includes a trait which assists in sending these notifications from
20
    | your application to your users. Feel free to explore this trait.
21
    |
22
    */
23
24
    use SendsPasswordResetEmails;
25
26
    /**
27
     * Send a reset link to the given user.
28
     *
29
     * @param  \Illuminate\Http\Request $request
30
     * @return \Illuminate\Http\RedirectResponse
31
     */
32
    public function sendResetLinkEmail(Request $request)
33
    {
34
        $this->validateEmail($request);
35
36
        // We will send the password reset link to this user. Once we have attempted
37
        // to send the link, we will examine the response then see the message we
38
        // need to show to the user. Finally, we'll send out a proper response.
39
        $response = $this->broker()->sendResetLink(
40
            $request->only('email')
41
        );
42
43
        return $response == Password::RESET_LINK_SENT
44
            ? $this->sendResetLinkResponse($request, $response)
45
            : $this->sendResetLinkFailedResponse($request, $response);
46
    }
47
48
    /**
49
     * Get the response for a successful password reset link.
50
     *
51
     * @param  string  $response
52
     * @return mixed
53
     */
54 View Code Duplication
    protected function sendResetLinkResponse(Request $request, $response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        if ($request->expectsJson()) {
57
            return response()->json([
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
58
                'status' => trans($response)
59
            ]);
60
        }
61
        return back()->with('status', trans($response));
62
    }
63
64
    /**
65
     * Get the response for a failed password reset link.
66
     *
67
     * @param Request $request
68
     * @param $response
69
     * @return mixed
70
     */
71
    protected function sendResetLinkFailedResponse(Request $request, $response)
72
    {
73
        if ($request->expectsJson()) {
74
            return new JsonResponse([
75
                'message' =>  'The given data was invalid.',
76
                'errors' => [
77
                    'email' => trans($response)
78
                ]
79
            ], 422);
80
        }
81
        return back()->withErrors(
82
            ['email' => trans($response)]
83
        );
84
    }
85
86
    /**
87
     * Display the form to request a password reset link.
88
     *
89
     * @return \Illuminate\Http\Response
90
     */
91
    public function showLinkRequestForm()
92
    {
93
        return view('adminlte::auth.passwords.email');
94
    }
95
96
    /**
97
     * Create a new controller instance.
98
     *
99
     * @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...
100
     */
101
    public function __construct()
102
    {
103
        $this->middleware('guest');
104
    }
105
}
106