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

StoreRequest::storeImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
ccs 7
cts 8
cp 0.875
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0078
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