Completed
Push — develop ( ad6f8c...e0a689 )
by Abdelrahman
02:06
created

AccountSettingsRequest::authorize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
            if ($password && mb_strlen($password) < config('rinvex.fort.password_min_chars')) {
77
                $validator->errors()->add('password', trans('validation.min.string', ['attribute' => 'password', 'min' => config('rinvex.fort.password_min_chars')]));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 166 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...
78
            }
79
        });
80
    }
81
82
    /**
83
     * Get the validation rules that apply to the request.
84
     *
85
     * @return array
86
     */
87
    public function rules()
88
    {
89
        $user = $this->user();
90
        $user->updateRulesUniques();
91
92
        return $user->getRules();
93
    }
94
}
95