Completed
Push — master ( 54d586...5aa3ec )
by Chris
14s queued 12s
created

ResetPasswordController::broker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Yab\FlightDeck\Http\Controllers;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
use Illuminate\Http\JsonResponse;
9
use Illuminate\Routing\Controller;
10
use Illuminate\Support\Facades\Auth;
11
use Illuminate\Support\Facades\Hash;
12
use Illuminate\Support\Facades\Password;
13
use Illuminate\Auth\Events\PasswordReset;
14
use Yab\FlightDeck\Http\Requests\ResetPasswordRequest;
15
16
class ResetPasswordController extends Controller
17
{
18
    /*
19
    |--------------------------------------------------------------------------
20
    | Password Reset Controller
21
    |--------------------------------------------------------------------------
22
    |
23
    | This controller is responsible for handling password reset requests
24
    | and uses a simple trait to include this behavior. You're free to
25
    | explore this trait and override any methods you wish to tweak.
26
    |
27
    */
28
29
    /**
30
     * Create a new controller instance.
31
     *
32
     * @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...
33
     */
34
    public function __construct()
35
    {
36
        $this->middleware('guest');
37
    }
38
39
    /**
40
     * Reset the given user's password.
41
     *
42
     * @param  Yab\FlightDeck\Http\Requests\ResetPasswordRequest  $request
43
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
44
     */
45
    public function reset(ResetPasswordRequest $request)
46
    {
47
        $response = $this->broker()->reset(
48
            $this->credentials($request),
49
            function ($user, $password) {
50
                $this->resetPassword($user, $password);
51
            }
52
        );
53
54
        return $response == Password::PASSWORD_RESET
55
            ? $this->sendResetResponse($request, $response)
56
            : $this->sendResetFailedResponse($request, $response);
57
    }
58
59
    /**
60
     * Get the response for a successful password reset.
61
     *
62
     * @param  Yab\FlightDeck\Http\Requests\ResetPasswordRequest   $request
63
     * @param  string  $response
64
     * @return \Illuminate\Http\JsonResponse
65
     */
66
    protected function sendResetResponse(ResetPasswordRequest $request, $response)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed.

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

Loading history...
67
    {
68
        return new JsonResponse([
69
            'success' => true,
70
            'message' => '',
71
        ], Response::HTTP_OK);
72
    }
73
74
    /**
75
     * Get the response for a failed password reset.
76
     *
77
     * @param  Yab\FlightDeck\Http\Requests\ResetPasswordRequest   $request
78
     * @param  string  $response
79
     * @return \Illuminate\Http\JsonResponse
80
     */
81
    protected function sendResetFailedResponse(ResetPasswordRequest $request, $response)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed.

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

Loading history...
82
    {
83
        return new JsonResponse([
84
            'success' => false,
85
            'message' => 'An error occurred while trying to reset the password',
86
        ], Response::HTTP_INTERNAL_SERVER_ERROR);
87
    }
88
89
    /**
90
     * Get the password reset credentials from the request.
91
     *
92
     * @param  \Illuminate\Http\Request  $request
93
     * @return array
94
     */
95
    protected function credentials(Request $request)
96
    {
97
        return $request->only(
98
            'email',
99
            'password',
100
            'password_confirmation',
101
            'token'
102
        );
103
    }
104
105
    /**
106
     * Set the user's password.
107
     *
108
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
109
     * @param  string  $password
110
     * @return void
111
     */
112
    protected function setUserPassword($user, $password)
113
    {
114
        $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?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
115
    }
116
117
    /**
118
     * Reset the given user's password.
119
     *
120
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
121
     * @param  string  $password
122
     * @return void
123
     */
124
    protected function resetPassword($user, $password)
125
    {
126
        $this->setUserPassword($user, $password);
127
128
        $user->setRememberToken(Str::random(60));
129
130
        $user->save();
131
132
        event(new PasswordReset($user));
0 ignored issues
show
Documentation introduced by
$user is of type object<Illuminate\Contra...\Auth\CanResetPassword>, but the function expects a object<Illuminate\Contracts\Auth\Authenticatable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
133
134
        $this->guard()->login($user);
0 ignored issues
show
Documentation introduced by
$user is of type object<Illuminate\Contra...\Auth\CanResetPassword>, but the function expects a object<Illuminate\Contracts\Auth\Authenticatable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
    }
136
137
    /**
138
     * Get the broker to be used during password reset.
139
     *
140
     * @return \Illuminate\Contracts\Auth\PasswordBroker
141
     */
142
    public function broker()
143
    {
144
        return Password::broker();
145
    }
146
147
    /**
148
     * Get the guard to be used during password reset.
149
     *
150
     * @return \Illuminate\Contracts\Auth\StatefulGuard
151
     */
152
    protected function guard()
153
    {
154
        return Auth::guard();
155
    }
156
}
157