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

UpdateRequest::attributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace App\Http\Requests\Denomination;
4
5
use App\Enum\DenominationType;
6
use App\Models\Denomination;
7
use Illuminate\Support\Facades\Storage;
8
use Illuminate\Validation\Rule;
9
10
class UpdateRequest extends AbstractRequest
11
{
12
    /**
13
     * {@inheritDoc}
14
     */
15 1
    public function rules()
16
    {
17
        return [
18 1
            'name' => 'required|string|max:255',
19 1
            'value' => ['required', 'numeric', 'min:0', Rule::unique(Denomination::class)->ignoreModel($this->route('denomination'))],
20
            'type' => 'required|enum:' . DenominationType::class,
21 1
            'quantity_per_bundle' => 'required|numeric|min:0',
22 1
            'minimum_order_bundle' => 'required|numeric|min:0',
23 1
            'maximum_order_bundle' => 'required|numeric|gte:minimum_order_bundle',
24 1
            'image' => 'sometimes|nullable|image',
25
        ];
26
    }
27
28
    /**
29
     * Update the image file from the incoming request.
30
     *
31
     * @param  string  $key
32
     * @return string|null
33
     */
34 1
    public function updateImage(string $key = 'image'): ?string
35
    {
36 1
        if (!$this->hasFile($key)) {
37
            return null;
38
        }
39
40
        /** @var \App\Models\Denomination $model */
41 1
        $model = $this->route('denomination');
42
43 1
        if ($model->getRawOriginal('image') && $this->hasFile($key)) {
44 1
            Storage::delete(Denomination::IMAGE_PATH . '/' . $model->getRawOriginal('image'));
0 ignored issues
show
Bug introduced by
Are you sure $model->getRawOriginal('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

44
            Storage::delete(Denomination::IMAGE_PATH . '/' . /** @scrutinizer ignore-type */ $model->getRawOriginal('image'));
Loading history...
45
        }
46
47 1
        $file = $this->file($key);
48
49 1
        $file->storeAs(
50 1
            Denomination::IMAGE_PATH,
51 1
            $filename = ($this->input('value') . '.' . $file->getClientOriginalExtension())
52
        );
53
54 1
        return $filename;
55
    }
56
}
57