Passed
Branch master (cd4548)
by Fèvre
19:36
created

VerificationController::verify()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
nc 9
nop 1
dl 0
loc 30
rs 8.8333
c 1
b 0
f 0
1
<?php
2
namespace Xetaravel\Http\Controllers\Auth;
3
4
use Illuminate\Auth\Access\AuthorizationException;
5
use Illuminate\Auth\Events\Verified;
6
use Illuminate\Foundation\Auth\VerifiesEmails;
7
use Illuminate\Http\JsonResponse;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Auth;
10
use Xetaravel\Http\Controllers\Controller;
11
use Xetaravel\Models\User;
12
use Xetaravel\Providers\RouteServiceProvider;
13
14
class VerificationController extends Controller
15
{
16
    /*
17
    |--------------------------------------------------------------------------
18
    | Email Verification Controller
19
    |--------------------------------------------------------------------------
20
    |
21
    | This controller is responsible for handling email verification for any
22
    | user that recently registered with the application. Emails may also
23
    | be re-sent if the user didn't receive the original email message.
24
    |
25
    */
26
27
    use VerifiesEmails;
28
29
    /**
30
     * Where to redirect users after verification.
31
     *
32
     * @var string
33
     */
34
    protected $redirectTo = 'users/login';
35
36
    /**
37
     * Create a new controller instance.
38
     *
39
     * @return void
40
     */
41
    public function __construct()
42
    {
43
        //$this->middleware('auth');
44
        $this->middleware('signed')->only('verify');
45
        $this->middleware('throttle:6,1')->only('verify', 'resend');
46
    }
47
48
    /**
49
     * Show the email verification notice.
50
     *
51
     * @param  \Illuminate\Http\Request  $request
52
     * @param string $hash The email of the user encoded to base64.
53
     *
54
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
55
     */
56
    public function show(Request $request, string $hash)
57
    {
58
        return view('Auth.verify', compact('hash'));
59
    }
60
61
    /**
62
     * Mark the authenticated user's email address as verified.
63
     *
64
     * @param  \Illuminate\Http\Request  $request
65
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
66
     *
67
     * @throws \Illuminate\Auth\Access\AuthorizationException
68
     */
69
    public function verify(Request $request)
70
    {
71
        $user = User::find($request->route('id'));
72
73
        if (!hash_equals((string) $request->route('hash'), sha1($user->getEmailForVerification()))) {
74
            throw new AuthorizationException;
75
        }
76
77
        if ($user->hasVerifiedEmail()) {
78
            return $request->wantsJson()
79
                        ? new JsonResponse([], 204)
80
                        : redirect($this->redirectPath());
81
        }
82
83
        if ($user->markEmailAsVerified()) {
84
            event(new Verified($user));
85
        }
86
87
        if ($response = $this->verified($request)) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $response is correct as $this->verified($request) targeting Xetaravel\Http\Controlle...nController::verified() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
88
            return $response;
89
        }
90
91
        // Login the user
92
        Auth::login($user, true);
93
94
        return $request->wantsJson()
95
                    ? new JsonResponse([], 204)
96
                    : redirect($this->redirectPath())
97
                        ->with('verified', true)
98
                        ->with('success', "Your Email has been verified!");
99
    }
100
101
    /**
102
     * Resend the email verification notification.
103
     *
104
     * @param  \Illuminate\Http\Request  $request
105
     *
106
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
107
     */
108
    public function resend(Request $request)
109
    {
110
        $id = base64_decode($request->input('hash'));
111
        $user = User::find($id);
112
113
        if ($user->hasVerifiedEmail()) {
114
            return $request->wantsJson()
115
                        ? new JsonResponse([], 204)
116
                        : redirect($this->redirectPath());
117
        }
118
119
        $user->sendEmailVerificationNotification();
120
121
        return $request->wantsJson()
122
                    ? new JsonResponse([], 202)
123
                    : back()->with('resent', true);
124
    }
125
}
126