Completed
Pull Request — master (#45)
by Şəhriyar
10:29
created

PasswordController::sendPasswordResetLink()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0072

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
ccs 12
cts 13
cp 0.9231
rs 8.6845
c 1
b 0
f 0
cc 4
eloc 13
nc 4
nop 1
crap 4.0072
1
<?php namespace App\Http\Controllers\Auth;
2
3
use App\Events\Users\RequestedResetPasswordLink;
4
use App\Events\Users\ResetPassword;
5
use App\Exceptions\Common\ValidationException;
6
use App\Exceptions\Users\TokenNotValidException;
7
use App\Http\Controllers\Controller;
8
use App\Models\User;
9
use App\Notifications\ResetPasswordNotification;
10
use Illuminate\Contracts\Auth\PasswordBroker;
11
use Illuminate\Database\Eloquent\ModelNotFoundException;
12
use Illuminate\Foundation\Auth\RedirectsUsers;
13
use Illuminate\Http\Request;
14
15
class PasswordController extends Controller
16
{
17
    use RedirectsUsers;
18
19
    /**
20
     * Create a new password controller instance.
21
     */
22 6
    public function __construct()
23
    {
24 6
        $this->middleware('guest');
25 6
    }
26
27
    /**
28
     * Display the form to request a password reset link.
29
     *
30
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
31
     */
32 2
    public function requestPasswordResetLink()
33
    {
34 2
        return view('password/email');
35
    }
36
37
    /**
38
     * Send a password reset link to the given email's owner, via email.
39
     *
40
     * @param \Illuminate\Http\Request $request
41
     *
42
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
43
     * @throws \App\Exceptions\Common\ValidationException
44
     */
45 2
    public function sendPasswordResetLink(Request $request)
46
    {
47 2
        $validator = app('validator')->make($request->all(), [
48
            'email' => 'required|email|max:255'
49 2
        ]);
50 2
        if ($validator->fails()) {
51 1
            throw new ValidationException($validator);
52
        }
53
54 2
        $user = User::whereEmail($request->only('email'))->first();
0 ignored issues
show
Bug introduced by
The method first does only exist in Illuminate\Database\Query\Builder, but not in App\Models\User.

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...
55 2
        if (is_null($user)) {
56 1
            throw new ModelNotFoundException(trans('passwords.user'));
57
        }
58
59 1
        $user->notify(new ResetPasswordNotification(app('auth.password.broker')->createToken($user)));
60
61 1
        event(new RequestedResetPasswordLink($user));
62
63 1
        if ($request->expectsJson()) {
64 1
            return response()->json(['message' => trans('passwords.sent')]);
65
        }
66
67
        return redirect()->back()->with('message', trans('passwords.sent'));
68
    }
69
70
    /**
71
     * Display the password reset view for the given token.
72
     *
73
     * @param \Illuminate\Http\Request $request
74
     * @param string                   $token
75
     *
76
     * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View|\Illuminate\Contracts\View\Factory
77
     */
78
    public function showPasswordResetForm(Request $request, $token = null)
79
    {
80
        if (is_null($token)) {
81
            if ($request->expectsJson()) {
82
                throw new TokenNotValidException();
83
            }
84
85
            return view('password/reset')->withErrors(['token' => trans(PasswordBroker::INVALID_TOKEN)]);
0 ignored issues
show
Bug introduced by
The method withErrors does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
86
        }
87
88
        if ($request->expectsJson()) {
89
            return response()->json(['token' => $token]);
90
        }
91
92
        return view('password/reset')->with('token', $token);
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
93
    }
94
95
    /**
96
     * Reset the password through password-reset-token and email provided.
97
     *
98
     * @param \Illuminate\Http\Request $request
99
     *
100
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
101
     * @throws \App\Exceptions\Common\ValidationException
102
     */
103 2
    public function resetPassword(Request $request)
104
    {
105 2
        $validator = app('validator')->make($request->all(), [
106 2
            'token' => 'required|string',
107 2
            'email' => 'required|email|max:255',
108 2
            'password' => 'required|confirmed|min:' . app('config')->get('auth.passwords.users.min_length')
109
        ]);
110 2
        if ($validator->fails()) {
111 1
            throw new ValidationException($validator);
112
        }
113
114 2
        $credentials = $request->only('email', 'password', 'password_confirmation', 'token');
115
116 2
        $passwordBroker = app('auth.password.broker');
117 2
        $response = $passwordBroker->reset(
118 2
            $credentials, function (User $user, $password) {
119 1
            $user->password = app('hash')->make($password);
120 1
            $user->save();
121 1
            app('auth.driver')->login($user);
122 2
        });
123
124
        switch ($response) {
125 2
            case $passwordBroker::INVALID_USER:
126 1
                throw new ModelNotFoundException(trans($response));
127
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
128 2
            case $passwordBroker::INVALID_TOKEN:
129 1
                throw new TokenNotValidException(trans($response));
130
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
131
        }
132
133 1
        event(new ResetPassword(app('auth.driver')->user()));
134
135 1
        if ($request->expectsJson()) {
136 1
            return response()->json(['message' => trans('passwords.reset')]);
137
        }
138
139
        return redirect($this->redirectPath())->with('message', trans('passwords.reset'));
140
    }
141
}
142