ResetPasswordController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 42
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A showResetForm() 0 10 2
A update() 0 12 2
A getUserFromEncryptedToken() 0 15 3
1
<?php
2
3
namespace Oscer\Cms\Backend\Http\Controllers\Auth;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Cache;
8
use Oscer\Cms\Backend\Http\Requests\Auth\ResetPasswordRequest;
9
use Oscer\Cms\Core\Models\User;
10
use Throwable;
11
12
class ResetPasswordController
13
{
14
    public function showResetForm(Request $request, $encryptedToken)
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...
15
    {
16
        $user = $this->getUserFromEncryptedToken($encryptedToken);
17
18
        if ($user === false) {
19
            return redirect()->route('cms.password.forgot')->with('invalidResetToken', true);
20
        }
21
22
        return view('cms::auth.reset-password', compact('encryptedToken', 'user'));
23
    }
24
25
    public function update(ResetPasswordRequest $request)
26
    {
27
        $user = $this->getUserFromEncryptedToken($request->input('encrypted_token'));
28
        if ($user === false) {
29
            return redirect()->route('cms.password.forgot')->with('invalidResetToken', true);
30
        }
31
        $user->update(['password' => $request->input('password')]);
32
33
        Auth::guard()->login($user);
34
35
        return redirect()->route('cms.backend.start');
36
    }
37
38
    protected function getUserFromEncryptedToken($encryptedToken)
39
    {
40
        try {
41
            $token = decrypt($encryptedToken);
42
            [$userId, $token] = explode('|', $token);
0 ignored issues
show
Bug introduced by
The variable $userId does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
43
            $user = User::query()->findOrFail($userId);
44
        } catch (Throwable $exception) {
45
            return false;
46
        }
47
        if (Cache::get("password.reset.{$userId}") != $token) {
48
            return false;
49
        }
50
51
        return $user;
52
    }
53
}
54