PasswordResetProcessRequest::withValidator()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.3222
c 0
b 0
f 0
cc 5
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Requests\Adminarea;
6
7
use Rinvex\Auth\Contracts\PasswordResetBrokerContract;
8
9
class PasswordResetProcessRequest extends PasswordResetRequest
10
{
11
    /**
12
     * Configure the validator instance.
13
     *
14
     * @param \Illuminate\Validation\Validator $validator
15
     *
16
     * @return void
17
     */
18
    public function withValidator($validator): void
19
    {
20
        $credentials = $this->only('email', 'expiration', 'token');
21
        $passwordResetBroker = app('auth.password')->broker($this->route('passwordResetBroker'));
22
23
        $validator->after(function ($validator) use ($passwordResetBroker, $credentials) {
24
            if (! ($user = $passwordResetBroker->getUser($credentials))) {
25
                $validator->errors()->add('email', trans('cortex/auth::'.PasswordResetBrokerContract::INVALID_USER));
26
            }
27
28
            if ($user && ! $passwordResetBroker->validateToken($user, $credentials)) {
29
                $validator->errors()->add('email', trans('cortex/auth::'.PasswordResetBrokerContract::INVALID_TOKEN));
30
            }
31
32
            if (! $passwordResetBroker->validateTimestamp($credentials['expiration'])) {
33
                $validator->errors()->add('email', trans('cortex/auth::'.PasswordResetBrokerContract::EXPIRED_TOKEN));
34
            }
35
        });
36
    }
37
38
    /**
39
     * Get the validation rules that apply to the request.
40
     *
41
     * @return array
42
     */
43
    public function rules(): array
44
    {
45
        return [
46
            // Do not validate `token` here since at this stage we can NOT generate viewable error,
47
            // and it is been processed in the controller through PasswordResetBroker anyway
48
            //'token' => 'required|regex:/^([0-9a-f]*)$/',
49
            'email' => 'required|email|min:3|max:150|exists:'.config('cortex.auth.tables.admins').',email',
50
        ];
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function getRedirectUrl()
57
    {
58
        return $this->redirector->getUrlGenerator()->route('adminarea.passwordreset.request');
59
    }
60
}
61