Passed
Push — develop ( b27e40...fcd18c )
by Septianata
43:54
created

UpdateRequest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 28
dl 0
loc 52
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A authorize() 0 3 1
A rules() 0 21 2
A attributes() 0 12 1
1
<?php
2
3
namespace App\Http\Requests\User;
4
5
use App\Enum\Gender;
6
use App\Infrastructure\Foundation\Http\FormRequest;
7
use App\Models\User;
8
use Illuminate\Validation\Rule;
9
use Illuminate\Validation\Rules;
10
use Propaganistas\LaravelPhone\PhoneNumber;
11
12
class UpdateRequest extends FormRequest
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17
    public function authorize()
18
    {
19
        return !is_null($this->user());
20
    }
21
22
    /**
23
     * {@inheritDoc}
24
     */
25
    public function rules()
26
    {
27
        return [
28
            'branch_id' => 'required|exists:branches,id',
29
            'username' => ['required', 'string', 'max:255', Rule::unique(User::class)->ignoreModel($this->route('user'))],
30
            'fullname' => 'required|string|max:255',
31
            'gender' => 'sometimes|nullable|enum:' . Gender::class,
32
            'email' => ['required', 'string', 'email', 'max:255', Rule::unique(User::class)->ignoreModel($this->route('user'))],
33
            'phone_country' => 'sometimes|in:ID',
34
            'phone' => ['required', 'string', 'phone:ID', function ($attribute, $phone, $fail) {
35
                $user = User::where('phone', PhoneNumber::make($phone, request()->input('phone_country', env('PHONE_COUNTRY', 'ID')))->formatE164())
36
                    ->where($this->route('user')->getKeyName(), '!=', $this->route('user')->getKey())
37
                    ->count();
38
39
                if ($user > 0) {
40
                    $fail(trans('validation.unique', ['attribute' => static::getAttributes()[$attribute]]));
41
                }
42
            }],
43
            'password' => ['sometimes', 'nullable', 'confirmed', Rules\Password::defaults()],
44
            'role' => 'required|exists:roles,name',
45
            'is_active' => 'required|boolean',
46
        ];
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function attributes()
53
    {
54
        return [
55
            'branch_id' => trans('admin-lang.branch'),
56
            'username' => trans('Username'),
57
            'fullname' => trans('Full name'),
58
            'gender' => trans('Gender'),
59
            'email' => trans('Email'),
60
            'phone_country' => trans('Phone Country'),
61
            'phone' => trans('Phone Number'),
62
            'role' => trans('Role'),
63
            'is_active' => trans('Active'),
64
        ];
65
    }
66
}
67