Passed
Push — develop ( 6dcea6...5ae9a6 )
by Septianata
16:24
created

StoreRequest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 32
c 1
b 0
f 0
dl 0
loc 60
ccs 24
cts 32
cp 0.75
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRules() 0 32 5
A storeImage() 0 14 2
1
<?php
2
3
namespace App\Http\Requests\Customer;
4
5
use App\Enum\Gender;
6
use App\Models\Customer;
7
use App\Rules\NotTelegramImage;
8
use Illuminate\Support\Str;
9
use Propaganistas\LaravelPhone\PhoneNumber;
10
11
class StoreRequest extends AbstractRequest
12
{
13
    /**
14
     * {@inheritDoc}
15
     */
16 1
    public static function getRules()
17
    {
18 1
        $phoneRule = function () {
19 1
            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 1
            }];
31
        };
32
33
        return [
34 1
            'telegram_chat_id' => 'required|string|max:255|unique:' . Customer::class,
35 1
            'username' => ['required', 'string', 'max:255', new NotTelegramImage],
36 1
            'fullname' => ['required', 'string', 'max:255', new NotTelegramImage],
37
            'gender' => 'sometimes|nullable|enum:' . Gender::class,
38 1
            'email' => ['required', 'string', 'email', 'max:255', 'unique:' . Customer::class, new NotTelegramImage],
39 1
            'phone_country' => 'sometimes|in:ID',
40 1
            'phone' => [value($phoneRule), new NotTelegramImage],
41 1
            'whatsapp_phone_country' => 'sometimes|in:ID',
42 1
            'whatsapp_phone' => [value($phoneRule), new NotTelegramImage],
43 1
            'account_number' => ['required_without:identitycard_number', new NotTelegramImage(canBeNull: true)] + (request()->filled('account_number') ? ['numeric'] : []),
44 1
            'identitycard_number' => ['required_without:account_number', new NotTelegramImage(canBeNull: true)] + (request()->filled('identitycard_number') ? ['numeric'] : []),
45 1
            'identitycard_image' => 'sometimes|nullable|image',
46 1
            'location_latitude' => ['required', 'numeric', new NotTelegramImage(false)],
47 1
            'location_longitude' => ['required', 'numeric', new NotTelegramImage(false)],
48
        ];
49
    }
50
51
    /**
52
     * Store the image file from the incoming request.
53
     *
54
     * @param  string  $key
55
     * @return string|null
56
     */
57 1
    public function storeImage(string $key = 'identitycard_image'): ?string
58
    {
59 1
        if (!$this->hasFile($key)) {
60
            return null;
61
        }
62
63 1
        $file = $this->file($key);
64
65 1
        $file->storeAs(
66 1
            Customer::IDENTITYCARD_IMAGE_PATH,
67 1
            $filename = (Str::random() . '.' . $file->getClientOriginalExtension())
68
        );
69
70 1
        return $filename;
71
    }
72
}
73