VerificationController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 117
rs 10
c 1
b 0
f 0
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A show() 0 3 1
A resend() 0 16 5
B verify() 0 35 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Http\Controllers\Auth;
6
7
use Illuminate\Auth\Access\AuthorizationException;
8
use Illuminate\Auth\Events\Verified;
9
use Illuminate\Contracts\View\Factory;
10
use Illuminate\Contracts\View\View;
11
use Illuminate\Foundation\Application;
12
use Illuminate\Foundation\Auth\VerifiesEmails;
13
use Illuminate\Http\JsonResponse;
14
use Illuminate\Http\RedirectResponse;
15
use Illuminate\Http\Request;
16
use Illuminate\Support\Facades\Auth;
17
use Xetaravel\Http\Controllers\Controller;
18
use Xetaravel\Models\User;
19
20
class VerificationController extends Controller
21
{
22
    /*
23
    |--------------------------------------------------------------------------
24
    | Email Verification Controller
25
    |--------------------------------------------------------------------------
26
    |
27
    | This controller is responsible for handling email verification for any
28
    | user that recently registered with the application. Emails may also
29
    | be re-sent if the user didn't receive the original email message.
30
    |
31
    */
32
33
    use VerifiesEmails;
34
35
    /**
36
     * Where to redirect users after verification.
37
     *
38
     * @var string
39
     */
40
    protected string $redirectTo = 'auth/login';
41
42
    /**
43
     * Create a new controller instance.
44
     *
45
     * @return void
46
     */
47
    public function __construct()
48
    {
49
        parent::__construct();
50
51
        $this->middleware('signed')->only('verify');
52
        $this->middleware('throttle:6,1')->only('verify', 'resend');
53
    }
54
55
    /**
56
     * Show the email verification notice.
57
     *
58
     * @param Request $request
59
     * @param string $hash The email of the user encoded to base64.
60
     *
61
     * @return Factory|View|Application|\Illuminate\View\View|object
62
     */
63
    public function show(Request $request, string $hash)
0 ignored issues
show
Unused Code introduced by
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

63
    public function show(/** @scrutinizer ignore-unused */ Request $request, string $hash)

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...
64
    {
65
        return view('Auth.verify', compact('hash'));
66
    }
67
68
    /**
69
     * Mark the authenticated user's email address as verified.
70
     *
71
     * @param Request $request
72
     *
73
     * @return JsonResponse|RedirectResponse
74
     *
75
     * @throws AuthorizationException
76
     */
77
    public function verify(Request $request)
78
    {
79
        $user = User::find($request->route('id'));
80
81
        if (!hash_equals((string) $request->route('id'), (string) $user->getKey())) {
82
            throw new AuthorizationException();
83
        }
84
85
        if (!hash_equals((string) $request->route('hash'), base64_encode($user->getEmailForVerification()))) {
86
            throw new AuthorizationException();
87
        }
88
89
        $user = User::find($request->route('id'));
90
91
        if ($user->hasVerifiedEmail()) {
92
            return $request->wantsJson()
93
                        ? new JsonResponse([], 204)
94
                        : redirect($this->redirectPath());
95
        }
96
97
        if ($user->markEmailAsVerified()) {
98
            event(new Verified($user));
99
        }
100
101
        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...
102
            return $response;
103
        }
104
105
        Auth::login($user, true);
106
107
        return $request->wantsJson()
108
                    ? new JsonResponse([], 204)
109
                    : redirect($this->redirectPath())
110
                        ->with('verified', true)
111
                        ->success("Your Email has been verified!");
112
    }
113
114
    /**
115
     * Resend the email verification notification.
116
     *
117
     * @param Request $request
118
     *
119
     * @return JsonResponse|RedirectResponse
120
     */
121
    public function resend(Request $request)
122
    {
123
        $email = base64_decode($request->input('hash'));
124
        $user = User::where('email', $email)->first();
125
126
        if (!$user || $user->hasVerifiedEmail()) {
127
            return $request->wantsJson()
128
                        ? new JsonResponse([], 204)
129
                        : redirect($this->redirectPath());
130
        }
131
132
        $user->sendEmailVerificationNotification();
133
134
        return $request->wantsJson()
135
                    ? new JsonResponse([], 202)
136
                    : back()->with('resent', true);
0 ignored issues
show
Bug introduced by
The method with() does not exist on Illuminate\Http\RedirectResponse. ( Ignorable by Annotation )

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

136
                    : back()->/** @scrutinizer ignore-call */ with('resent', true);

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...
137
    }
138
}
139