Passed
Push — master ( 72576e...ceb7cb )
by Paul
04:15
created

Http/Controllers/Auth/ChangeEmailController.php (2 issues)

1
<?php
2
3
namespace Devpri\Tinre\Http\Controllers\Auth;
4
5
use Carbon\Carbon;
6
use Devpri\Tinre\Events\EmailChangeCreated;
7
use Devpri\Tinre\Http\Controllers\Controller;
8
use Exception;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\DB;
11
use Illuminate\Support\Facades\Hash;
12
use Illuminate\Support\Str;
13
14
class ChangeEmailController extends Controller
15
{
16
    /*
17
    |--------------------------------------------------------------------------
18
    | Change Email Controller
19
    |--------------------------------------------------------------------------
20
    |
21
    | This controller handles user email changes.
22
    |
23
    */
24
25 1
    public function __construct()
26
    {
27 1
        $this->middleware('auth')->only('create');
28 1
    }
29
30
    /**
31
     * Where to redirect users after verification.
32
     */
33 1
    public function redirectTo()
34
    {
35 1
        return route('dashboard');
36
    }
37
38 1
    public function create(Request $request)
39
    {
40 1
        $this->validateData($request);
41
42 1
        $user = $request->user();
43
44 1
        if (! Hash::check($request->password, $user->password)) {
45
            return response()->json(['message' => __('Invalid Password')], 403);
46
        }
47
48 1
        $maxGenerationAttempts = 5;
49
50 1
        while ($maxGenerationAttempts-- > 0) {
51
            try {
52
                $data = [
53 1
                    'email' => $request->email,
54 1
                    'token' => Str::random(60),
55 1
                    'created_at' => Carbon::now(),
56
                ];
57
58 1
                DB::table('email_changes')->updateOrInsert(['user_id' => $user->id], $data);
59
            } catch (Exception $e) {
60
                if ($maxGenerationAttempts === 0) {
61
                    throw $e;
62
                }
63
            }
64
        }
65
66 1
        event(new EmailChangeCreated($data));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.
Loading history...
67
68 1
        return response()->json(['message' => __('Please verify your email.')]);
69
    }
70
71 1
    public function change(Request $request, $token)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

71
    public function change(/** @scrutinizer ignore-unused */ Request $request, $token)

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

Loading history...
72
    {
73 1
        $email = DB::table('email_changes')->where('token', $token)->first();
74
75 1
        if (! $email) {
76
            abort(404);
77
        }
78
79 1
        DB::table('users')->where('id', $email->user_id)->update([
80 1
            'email' => $email->email,
81 1
            'email_verified_at' => Carbon::now(),
82
        ]);
83
84 1
        DB::table('email_changes')->where('token', $token)->delete();
85
86 1
        return redirect($this->redirectTo())->with('status', __('Email changed.'));
87
    }
88
89 1
    protected function validateData(Request $request)
90
    {
91 1
        $request->validate([
92 1
            'email' => ['required', 'email', 'unique:users'],
93
            'password' => ['required'],
94
        ]);
95 1
    }
96
}
97