Completed
Push — develop ( 676a40...1f1713 )
by Abdelrahman
02:04
created

EmailVerificationProcessRequest::authorize()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 8.2222
c 1
b 0
f 0
cc 7
eloc 6
nc 12
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Http\Requests\Frontend;
6
7
use Cortex\Fort\Models\User;
8
use Rinvex\Support\Http\Requests\FormRequest;
9
use Cortex\Foundation\Exceptions\GenericException;
10
11
class EmailVerificationProcessRequest extends FormRequest
12
{
13
    /**
14
     * Determine if the user is authorized to make this request.
15
     *
16
     * @throws \Cortex\Foundation\Exceptions\GenericException
17
     *
18
     * @return bool
19
     */
20
    public function authorize()
21
    {
22
        $userVerified = $this->user() && $this->user()->email_verified;
23
        $guestVerified = ($email = $this->get('email')) && ($user = User::where('email', $email)->first()) && $user->email_verified;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
24
25
        if ($userVerified || $guestVerified) {
26
            // Redirect users if their email already verified, no need to process their request
27
            throw new GenericException(trans('cortex/fort::messages.verification.email.already'), $userVerified ? route('frontend.account.settings') : route('frontend.auth.login'));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 181 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
28
        }
29
30
        return true;
31
    }
32
33
    /**
34
     * Get the validation rules that apply to the request.
35
     *
36
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
37
     */
38
    public function rules()
39
    {
40
        // Do not validate `token` here since at this stage we can NOT generate viewable
41
        // notification/error, and it is been processed through EmailVerificationBroker anyway
42
        return ['email' => 'required|email|max:250|exists:'.config('rinvex.fort.tables.users').',email'];
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function getRedirectUrl()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
49
    {
50
        // This is route('frontend.verification.email.send')
51
        if ($this->isMethod('post')) {
52
            return parent::getRedirectUrl();
53
        }
54
55
        // This is route('frontend.verification.email.verify')
56
        return $this->redirector->getUrlGenerator()->route('frontend.verification.email.request');
57
    }
58
}
59