Completed
Push — master ( 3e8f18...c69213 )
by Abdelrahman
02:02
created

AccountSettingsRequest::withValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Requests\Adminarea;
6
7
use Rinvex\Support\Traits\Escaper;
8
use Illuminate\Foundation\Http\FormRequest;
9
10
class AccountSettingsRequest extends FormRequest
11
{
12
    use Escaper;
13
14
    /**
15
     * Determine if the user is authorized to make this request.
16
     *
17
     * @return bool
18
     */
19
    public function authorize(): bool
20
    {
21
        return true;
22
    }
23
24
    /**
25
     * Prepare the data for validation.
26
     *
27
     * @return void
28
     */
29
    protected function prepareForValidation(): void
30
    {
31
        $data = $this->all();
32
33
        $country = $data['country_code'] ?? null;
34
        $email = $data['email'] ?? null;
35
        $phone = $data['phone'] ?? null;
36
        $user = $this->user($this->route('guard'));
37
        $twoFactor = $user->getTwoFactor();
38
39
        if ($email !== $user->email) {
40
            $data['email_verified_at'] = null;
41
        }
42
43
        if ($phone !== $user->phone || $country !== $user->country_code) {
44
            $data['phone_verified_at'] = null;
45
        }
46
47
        if ($twoFactor || is_null($data['phone_verified_at'])) {
48
            array_set($twoFactor, 'phone.enabled', false);
49
            $data['two_factor'] = $twoFactor;
50
        }
51
52
        $this->replace($data);
53
    }
54
55
    /**
56
     * Get the validation rules that apply to the request.
57
     *
58
     * @return array
59
     */
60
    public function rules(): array
61
    {
62
        $user = $this->user($this->route('guard'));
63
        $user->updateRulesUniques();
64
65
        return $user->getRules();
66
    }
67
}
68