Passed
Push — develop ( 9324e6...f3eb57 )
by Septianata
04:31
created

StoreRequest::getRules()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5.002

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 32
ccs 22
cts 23
cp 0.9565
rs 9.2248
cc 5
nc 4
nop 0
crap 5.002
1
<?php
2
3
namespace App\Http\Requests\Customer;
4
5
use App\Enum\Gender;
6
use App\Models\Customer;
7
use Illuminate\Support\Str;
8
use Propaganistas\LaravelPhone\PhoneNumber;
9
10
class StoreRequest extends AbstractRequest
11
{
12
    /**
13
     * {@inheritDoc}
14
     */
15 1
    public static function getRules()
16
    {
17 1
        $phoneRule = function () {
18 1
            return ['required', 'string', 'phone:ID', function ($attribute, $phone, $fail) {
19 1
                $country = $attribute === 'phone'
20 1
                    ? request()->input('phone_country', env('PHONE_COUNTRY', 'ID'))
21 1
                    : request()->input('whatsapp_phone_country', env('PHONE_COUNTRY', 'ID'));
22
23 1
                $user = Customer::where($attribute, PhoneNumber::make($phone, $country)->formatE164())
24 1
                    ->count();
25
26 1
                if ($user > 0) {
27
                    $fail(trans('validation.unique', ['attribute' => static::getAttributes()[$attribute]]));
28
                }
29 1
            }];
30
        };
31
32
        return [
33 1
            'telegram_chat_id' => 'required|string|max:255|unique:' . Customer::class,
34 1
            'username' => 'required|string|max:255',
35 1
            'fullname' => 'required|string|max:255',
36
            'gender' => 'sometimes|nullable|enum:' . Gender::class,
37
            'email' => 'required|string|email|max:255|unique:' . Customer::class,
38 1
            'phone_country' => 'sometimes|in:ID',
39 1
            'phone' => value($phoneRule),
40 1
            'whatsapp_phone_country' => 'sometimes|in:ID',
41 1
            'whatsapp_phone' => value($phoneRule),
42 1
            'account_number' => ['required_without:identitycard_number'] + (request()->filled('account_number') ? ['numeric'] : []),
43 1
            'identitycard_number' => ['required_without:account_number'] + (request()->filled('identitycard_number') ? ['numeric'] : []),
44 1
            'identitycard_image' => 'sometimes|nullable|image',
45 1
            'location_latitude' => 'required|numeric',
46 1
            'location_longitude' => 'required|numeric',
47
        ];
48
    }
49
50
    /**
51
     * Store the image file from the incoming request.
52
     *
53
     * @param  string  $key
54
     * @return string|null
55
     */
56 1
    public function storeImage(string $key = 'identitycard_image'): ?string
57
    {
58 1
        if (!$this->hasFile($key)) {
59
            return null;
60
        }
61
62 1
        $file = $this->file($key);
63
64 1
        $file->storeAs(
65 1
            Customer::IDENTITYCARD_IMAGE_PATH,
66 1
            $filename = (Str::random() . '.' . $file->getClientOriginalExtension())
67
        );
68
69 1
        return $filename;
70
    }
71
}
72