PasswordsController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 24
c 3
b 2
f 0
dl 0
loc 60
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A token() 0 19 4
A __construct() 0 3 1
A reset() 0 23 4
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Requests\PasswordTokenRequest;
6
use App\Models\PasswordReset;
7
use App\Models\User;
8
use App\Notifications\PasswordResetRequest;
9
use App\Notifications\PasswordResetSuccess;
10
use App\Services\TenantManager;
11
use Illuminate\Support\Str;
12
13
class PasswordsController extends Controller
14
{
15
    protected $tenantManager;
16
17
    /**
18
     * Create a new controller instance.
19
     *
20
     * @param TenantManager $tenantManager
21
     *
22
     * @return void
23
     */
24
    public function __construct(TenantManager $tenantManager)
25
    {
26
        $this->tenantManager = $tenantManager;
27
    }
28
29
    public function token(PasswordTokenRequest $request)
30
    {
31
        if ($this->tenantManager->loadTenantByUsername($request->email)) {
0 ignored issues
show
Bug introduced by
The property email does not seem to exist on App\Http\Requests\PasswordTokenRequest.
Loading history...
32
            $user = User::where('email', $request->email)->first();
33
34
            if ($user) {
35
                $passwordReset = PasswordReset::updateOrCreate(
36
                    ['email' => $user->email],
37
                    ['email' => $user->email, 'token' => Str::random(60)]
0 ignored issues
show
Bug introduced by
The method random() does not exist on Illuminate\Support\Str. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
                    ['email' => $user->email, 'token' => Str::/** @scrutinizer ignore-call */ random(60)]

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
                );
39
40
                if ($passwordReset) {
41
                    $user->notify(new PasswordResetRequest($passwordReset->token));
42
                }
43
            }
44
        }
45
46
        //always empty response regardless of actual result (for security reasons)
47
        return response()->json();
48
    }
49
50
    public function reset(\App\Http\Requests\PasswordResetRequest $request)
51
    {
52
        $passwordReset = PasswordReset::where('token', $request->get('token'))->first();
0 ignored issues
show
Bug introduced by
The method get() does not exist on App\Http\Requests\PasswordResetRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        $passwordReset = PasswordReset::where('token', $request->/** @scrutinizer ignore-call */ get('token'))->first();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
54
        if (!$passwordReset) {
55
            return response()->json(['message' => 'This password reset token is invalid.'], 404);
56
        }
57
58
        if ($this->tenantManager->loadTenantByUsername($passwordReset->email)) {
59
            $user = User::where('email', $passwordReset->email)->first();
60
61
            if (!$user) {
62
                return response()->json(['message' => 'We can\'t find a user with that e-mail address.'], 404);
63
            }
64
65
            $user->password = bcrypt($request->get('password'));
66
            $user->save();
67
            $passwordReset->delete();
68
69
            $user->notify(new PasswordResetSuccess());
70
        }
71
72
        return response()->json(['message' => 'Password has been changed.']);
73
    }
74
}
75