Passed
Push — develop ( e34a97...d968c2 )
by Septianata
14:57
created

UpdateRequest::getRules()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 38
ccs 0
cts 28
cp 0
rs 9.504
cc 3
nc 1
nop 0
crap 12
1
<?php
2
3
namespace App\Http\Requests\Customer;
4
5
use App\Enum\Gender;
6
use App\Infrastructure\Foundation\Http\FormRequest;
7
use App\Models\Customer;
8
use Illuminate\Support\Facades\Validator;
9
use Illuminate\Validation\Rule;
10
use Propaganistas\LaravelPhone\PhoneNumber;
11
12
class UpdateRequest extends FormRequest
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17
    public static function getRules()
18
    {
19
        $phoneRule = function () {
20
            return ['required', 'string', 'phone:ID', function ($attribute, $phone, $fail) {
21
                $validator = Validator::make(compact('phone'), [
22
                    'phone' => Rule::unique(Customer::class)->where(function ($query) use ($phone) {
23
                        $query->where('phone', PhoneNumber::make($phone, 'ID')->formatE164());
24
                    })->ignore($phone, 'phone'),
25
                ]);
26
27
                if ($validator->fails()) {
28
                    $fail(trans('validation.unique', compact('attribute')));
29
                }
30
            }];
31
        };
32
33
        return [
34
            'username' => 'required|string|max:255',
35
            'fullname' => 'required|string|max:255',
36
            'gender' => ['sometimes', 'nullable', Rule::in(Gender::toValues())],
37
            'email' => ['required', 'string', 'email', 'max:255', function ($attribute, $email, $fail) {
38
                $validator = Validator::make(compact('email'), [
39
                    'email' => Rule::unique(Customer::class)->ignore($email, 'email'),
40
                ]);
41
42
                if ($validator->fails()) {
43
                    $fail(trans('validation.unique', compact('attribute')));
44
                }
45
            }],
46
            'phone_country' => 'sometimes|in:ID',
47
            'phone' => value($phoneRule),
48
            'whatsapp_phone_country' => 'sometimes|in:ID',
49
            'whatsapp_phone' => value($phoneRule),
50
            'account_number' => 'required_without:identitycard_number|numeric',
51
            'identitycard_number' => 'required_without:account_number|numeric',
52
            'identitycard_image' => 'sometimes|nullable|image',
53
            'location_latitude' => 'sometimes|nullable|numeric',
54
            'location_longitude' => 'sometimes|nullable|numeric',
55
        ];
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    public static function getAttributes()
62
    {
63
        return [
64
            'username' => trans('Username'),
65
            'fullname' => trans('Full name'),
66
            'gender' => trans('Gender'),
67
            'email' => trans('Email'),
68
            'phone_country' => trans('Phone Country'),
69
            'phone' => trans('Phone Number'),
70
            'whatsapp_phone_country' => trans('Whatsapp Phone Country'),
71
            'whatsapp_phone' => trans('Whatsapp Phone Number'),
72
            'account_number' => trans('Account Number'),
73
            'identitycard_number' => trans('Identity Card Number'),
74
            'identitycard_image' => trans('Identity Card Image'),
75
            'location_latitude' => trans('Latitude'),
76
            'location_longitude' => trans('Longitude'),
77
        ];
78
    }
79
}
80