Passed
Push — develop ( fcd18c...7888af )
by Septianata
11:22
created

StoreRequest::storeImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 0
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
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\Str;
9
use Propaganistas\LaravelPhone\PhoneNumber;
10
11
class StoreRequest extends FormRequest
12
{
13
    /**
14
     * {@inheritDoc}
15
     */
16
    public static function getRules()
17
    {
18
        $phoneRule = function () {
19
            return ['required', 'string', 'phone:ID', function ($attribute, $phone, $fail) {
20
                $country = $attribute === 'phone'
21
                    ? request()->input('phone_country', env('PHONE_COUNTRY', 'ID'))
22
                    : request()->input('whatsapp_phone_country', env('PHONE_COUNTRY', 'ID'));
23
24
                $user = Customer::where($attribute, PhoneNumber::make($phone, $country)->formatE164())
25
                    ->count();
26
27
                if ($user > 0) {
28
                    $fail(trans('validation.unique', ['attribute' => static::getAttributes()[$attribute]]));
29
                }
30
            }];
31
        };
32
33
        return [
34
            'username' => 'required|string|max:255',
35
            'fullname' => 'required|string|max:255',
36
            'gender' => 'sometimes|nullable|enum:' . Gender::class,
37
            'email' => 'required|string|email|max:255|unique:' . Customer::class,
38
            'phone_country' => 'sometimes|in:ID',
39
            'phone' => value($phoneRule),
40
            'whatsapp_phone_country' => 'sometimes|in:ID',
41
            'whatsapp_phone' => value($phoneRule),
42
            'account_number' => ['required_without:identitycard_number'] + (request()->filled('account_number') ? ['numeric'] : []),
43
            'identitycard_number' => ['required_without:account_number'] + (request()->filled('identitycard_number') ? ['numeric'] : []),
44
            'identitycard_image' => 'sometimes|nullable|image',
45
            'location_latitude' => 'required|numeric',
46
            'location_longitude' => 'required|numeric',
47
        ];
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public static function getAttributes()
54
    {
55
        return [
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
            'whatsapp_phone_country' => trans('Whatsapp Phone Country'),
63
            'whatsapp_phone' => trans('Whatsapp Phone Number'),
64
            'account_number' => trans('Account Number'),
65
            'identitycard_number' => trans('Identity Card Number'),
66
            'identitycard_image' => trans('Identity Card Image'),
67
            'location_latitude' => trans('Latitude'),
68
            'location_longitude' => trans('Longitude'),
69
        ];
70
    }
71
72
    /**
73
     * Store the image file from the incoming request.
74
     *
75
     * @param  string  $key
76
     * @return string|null
77
     */
78
    public function storeImage(string $key = 'identitycard_image'): ?string
79
    {
80
        if (!$this->hasFile($key)) {
81
            return null;
82
        }
83
84
        $file = $this->file($key);
85
86
        $file->storeAs(
87
            Customer::IDENTITYCARD_IMAGE_PATH,
88
            $filename = (Str::random() . '.' . $file->getClientOriginalExtension())
89
        );
90
91
        return $filename;
92
    }
93
}
94