Passed
Push — master ( 12989d...3ff895 )
by Dan Michael O.
01:53
created

UserUpsertRequest::withValidator()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 30
rs 9.2888
1
<?php
2
3
namespace App\Http\Requests;
4
5
use App\UserIdentifier;
6
use Illuminate\Foundation\Http\FormRequest;
0 ignored issues
show
Bug introduced by
The type Illuminate\Foundation\Http\FormRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Validation\Validator;
0 ignored issues
show
Bug introduced by
The type Illuminate\Validation\Validator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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