Completed
Push — develop ( 735a56...964561 )
by Abdelrahman
03:04 queued 58s
created

AccountSettingsRequest::withValidator()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 6
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Fort\Http\Requests\Frontend;
6
7
use Rinvex\Support\Http\Requests\FormRequest;
8
9
class AccountSettingsRequest extends FormRequest
10
{
11
    /**
12
     * Determine if the user is authorized to make this request.
13
     *
14
     * @return bool
15
     */
16
    public function authorize()
17
    {
18
        return true;
19
    }
20
21
    /**
22
     * Process given request data before validation.
23
     *
24
     * @param array $data
25
     *
26
     * @return array
27
     */
28
    public function process($data)
29
    {
30
        $country = $data['country_code'] ?? null;
31
        $password = $data['password'] ?? null;
32
        $email = $data['email'] ?? null;
33
        $phone = $data['phone'] ?? null;
34
        $user = $this->user();
35
        $twoFactor = $user->getTwoFactor();
36
37
        if (! $password) {
38
            unset($data['password'], $data['password_confirmation']);
39
        }
40
41
        if ($email !== $user->email) {
42
            $data['email_verified'] = false;
43
            $data['email_verified_at'] = null;
44
        }
45
46
        if ($phone !== $user->phone) {
47
            $data['phone_verified'] = false;
48
            $data['phone_verified_at'] = null;
49
        }
50
51
        if ($twoFactor && (isset($data['phone_verified']) || $country !== $user->country_code)) {
52
            array_set($twoFactor, 'phone.enabled', false);
53
            $data['two_factor'] = $twoFactor;
54
        }
55
56
        return $data;
57
    }
58
59
    /**
60
     * Configure the validator instance.
61
     *
62
     * @param \Illuminate\Validation\Validator $validator
63
     *
64
     * @return void
65
     */
66
    public function withValidator($validator)
67
    {
68
        $validator->after(function ($validator) {
69
            $data = $this->all();
70
            $password = $data['password'] ?? null;
71
72
            if ($password && $password !== $data['password_confirmation']) {
73
                $validator->errors()->add('password', trans('validation.confirmed', ['attribute' => 'password']));
74
            }
75
        });
76
    }
77
78
    /**
79
     * Get the validation rules that apply to the request.
80
     *
81
     * @return array
82
     */
83
    public function rules()
84
    {
85
        $user = $this->user();
86
        $user->updateRulesUniques();
87
88
        return $user->getRules();
89
    }
90
}
91