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

UpdateRequest::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 16
ccs 14
cts 14
cp 1
rs 9.7998
c 1
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace App\Http\Requests\Customer;
4
5
use App\Enum\Gender;
6
use App\Models\Customer;
7
use Illuminate\Support\Facades\Storage;
8
use Illuminate\Support\Facades\Validator;
9
use Illuminate\Validation\Rule;
10
use Propaganistas\LaravelPhone\PhoneNumber;
11
12
class UpdateRequest extends AbstractRequest
13
{
14
    /**
15
     * {@inheritDoc}
16
     */
17 1
    public static function getRules()
18
    {
19 1
        $phoneRule = function () {
20 1
            return ['required', 'string', 'phone:ID', function ($attribute, $phone, $fail) {
21 1
                $country = $attribute === 'phone'
22 1
                    ? request()->input('phone_country', env('PHONE_COUNTRY', 'ID'))
23 1
                    : request()->input('whatsapp_phone_country', env('PHONE_COUNTRY', 'ID'));
24
25 1
                $user = Customer::where($attribute, PhoneNumber::make($phone, $country)->formatE164())
26 1
                    ->where(request()->route('customer')->getKeyName(), '!=', request()->route('customer')->getKey())
27 1
                    ->count();
28
29 1
                if ($user > 0) {
30
                    $fail(trans('validation.unique', ['attribute' => static::getAttributes()[$attribute]]));
31
                }
32 1
            }];
33
        };
34
35
        return [
36 1
            'telegram_chat_id' => ['required', 'string', 'max:255', function ($attribute, $telegram_chat_id, $fail) {
37 1
                $validator = Validator::make(compact('telegram_chat_id'), [
38 1
                    $attribute => Rule::unique(Customer::class)->ignore($telegram_chat_id, $attribute),
39
                ]);
40
41 1
                if ($validator->fails()) {
42
                    $fail(trans('validation.unique', compact('attribute')));
43
                }
44 1
            }],
45 1
            'username' => 'required|string|max:255',
46 1
            'fullname' => 'required|string|max:255',
47
            'gender' => 'sometimes|nullable|enum:' . Gender::class,
48 1
            'email' => ['required', 'string', 'email', 'max:255', function ($attribute, $email, $fail) {
49 1
                $validator = Validator::make(compact('email'), [
50 1
                    $attribute => Rule::unique(Customer::class)->ignore($email, $attribute),
51
                ]);
52
53 1
                if ($validator->fails()) {
54
                    $fail(trans('validation.unique', compact('attribute')));
55
                }
56 1
            }],
57 1
            'phone_country' => 'sometimes|in:ID',
58 1
            'phone' => value($phoneRule),
59 1
            'whatsapp_phone_country' => 'sometimes|in:ID',
60 1
            'whatsapp_phone' => value($phoneRule),
61 1
            'account_number' => ['required_without:identitycard_number'] + (request()->filled('account_number') ? ['numeric'] : []),
62 1
            'identitycard_number' => ['required_without:account_number'] + (request()->filled('identitycard_number') ? ['numeric'] : []),
63 1
            'identitycard_image' => 'sometimes|nullable|image',
64 1
            'location_latitude' => 'required|numeric',
65 1
            'location_longitude' => 'required|numeric',
66
        ];
67
    }
68
69
    /**
70
     * Update the image file from the incoming request.
71
     *
72
     * @param  string  $key
73
     * @return string|null
74
     */
75 1
    public function updateImage(string $key = 'identitycard_image'): ?string
76
    {
77 1
        if (!$this->hasFile($key)) {
78
            return null;
79
        }
80
81
        /** @var \App\Models\Customer $model */
82 1
        $model = $this->route('customer');
83
84 1
        if ($model->getRawOriginal('identitycard_image') && $this->hasFile($key)) {
85 1
            Storage::delete(Customer::IDENTITYCARD_IMAGE_PATH . '/' . $model->getRawOriginal('identitycard_image'));
0 ignored issues
show
Bug introduced by
Are you sure $model->getRawOriginal('identitycard_image') of type array|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
            Storage::delete(Customer::IDENTITYCARD_IMAGE_PATH . '/' . /** @scrutinizer ignore-type */ $model->getRawOriginal('identitycard_image'));
Loading history...
86
        }
87
88 1
        $file = $this->file($key);
89
90 1
        $file->storeAs(
91 1
            Customer::IDENTITYCARD_IMAGE_PATH,
92 1
            $filename = ($this->input('value') . '.' . $file->getClientOriginalExtension())
93
        );
94
95 1
        return $filename;
96
    }
97
}
98