Passed
Push — develop ( dcecff...37402c )
by Septianata
16:01
created

UpdateRequest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 24
c 1
b 0
f 0
dl 0
loc 50
ccs 22
cts 23
cp 0.9565
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateImage() 0 21 4
A rules() 0 15 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
            'key' => ['required', 'string', 'max:255', Rule::unique(Denomination::class)->ignoreModel($this->route('denomination'))],
19 1
            'name' => 'required|string|max:255',
20 1
            'value' => 'required|numeric|min:0',
21
            'type' => 'required|enum:' . DenominationType::class,
22 1
            'quantity_per_bundle' => 'required|numeric|min:0',
23 1
            'minimum_order_bundle' => 'required|numeric|min:0',
24 1
            'maximum_order_bundle' => 'required|numeric|gte:minimum_order_bundle',
25 1
            'minimum_order_quantity' => 'sometimes|nullable|numeric|min:0',
26 1
            'maximum_order_quantity' => 'sometimes|nullable|numeric|gte:minimum_order_quantity',
27 1
            'can_order_custom_quantity' => 'required|boolean',
28 1
            'is_visible' => 'required|boolean',
29 1
            'image' => 'sometimes|nullable|image',
30
        ];
31
    }
32
33
    /**
34
     * Update the image file from the incoming request.
35
     *
36
     * @param  string  $key
37
     * @return string|null
38
     */
39 1
    public function updateImage(string $key = 'image'): ?string
40
    {
41 1
        if (!$this->hasFile($key)) {
42
            return null;
43
        }
44
45
        /** @var \App\Models\Denomination $model */
46 1
        $model = $this->route('denomination');
47
48 1
        if ($model->getRawOriginal('image') && $this->hasFile($key)) {
49 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

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