UserUpsertRequest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A messages() 0 10 1
A withValidator() 0 30 5
A rules() 0 9 1
1
<?php
2
3
namespace App\Http\Requests;
4
5
use App\UserIdentifier;
6
use Illuminate\Foundation\Http\FormRequest;
7
use Illuminate\Validation\Validator;
8
9
class UserUpsertRequest extends FormRequest
10
{
11
    public $identifiers;
12
13
    /**
14
     * Get the error messages for the defined validation rules.
15
     *
16
     * @return array
17
     */
18
    public function messages()
19
    {
20
        return [
21
            'lastname.required' => 'Etternavn må fylles inn.',
22
            'firstname.required' => 'Fornavn må fylles inn.',
23
            'email.required_without' => 'Enten e-post eller telefonnummer må fylles inn.',
24
            'email.email' => 'E-postadresse må se ut som en e-postadresse.',
25
            'phone.regex' => 'Telefonnummeret må inneholde minst 8 tall, ' .
26
                'og ingen ikke-numeriske tegn bortsett fra et eventuelt plusstegn først.',
27
            'lang.required' => 'Språk må fylles inn.'
28
        ];
29
    }
30
31
    /**
32
     * Get the validation rules that apply to the request.
33
     *
34
     * @return array
35
     */
36
    public function rules()
37
    {
38
        return [
39
            'lastname' => 'required',
40
            'firstname' => 'required',
41
            'phone' => 'nullable|regex:/^\+?[0-9]{8,}$/',
42
            'email' => 'nullable|email|required_without:phone',
43
            'note' => 'nullable',
44
            'lang' => 'required|in:eng,nob,nno',
45
        ];
46
    }
47
48
    /**
49
     * Configure the validator instance.
50
     *
51
     * @param  Validator  $validator
52
     * @return void
53
     */
54
    public function withValidator(Validator $validator)
55
    {
56
        $validator->after(function (Validator $validator) {
57
            $userId = $this->route('user')->id;
58
            $identifiers = [];
59
            foreach ($this->all() as $key => $val) {
60
                if (preg_match('/identifier_type_(new|[0-9]+)/', $key, $matches)) {
61
                    $id = $matches[1];
62
                    $identifier = [
63
                        'type' => $this->input('identifier_type_' . $id),
64
                        'value' => $this->input('identifier_value_' . $id),
65
                    ];
66
                    if (empty($identifier['value'])) {
67
                        continue;
68
                    }
69
                    $res = UserIdentifier::where('value', '=', $identifier['value'])
70
                        ->where('user_id', '!=', $userId)
71
                        ->first();
72
73
                    if (!is_null($res)) {
74
                        $validator->errors()->add(
75
                            'identifier_value_' . $id,
76
                            "Identifikatoren «{$identifier['value']}» er allerede i bruk av en annen bruker!"
77
                        );
78
                    }
79
80
                    $identifiers[] = $identifier;
81
                }
82
            }
83
            $this->identifiers = $identifiers;
84
        });
85
    }
86
}
87