Passed
Push — develop ( 6302a9...3db2fb )
by Septianata
04:45
created

UpdateRequest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
dl 0
loc 63
ccs 22
cts 23
cp 0.9565
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A updateImage() 0 21 4
A attributes() 0 10 1
A rules() 0 3 1
A authorize() 0 3 1
1
<?php
2
3
namespace App\Http\Requests\Denomination;
4
5
use App\Infrastructure\Foundation\Http\FormRequest;
6
use App\Models\Denomination;
7
use Illuminate\Support\Facades\Storage;
8
9
class UpdateRequest extends FormRequest
10
{
11
    /**
12
     * {@inheritDoc}
13
     */
14 1
    public function authorize()
15
    {
16 1
        return !is_null($this->user());
17
    }
18
19
    /**
20
     * {@inheritDoc}
21
     */
22 1
    public function rules()
23
    {
24
        return [
25
            //
26 1
        ];
27
    }
28
29
    /**
30
     * {@inheritDoc}
31
     */
32 1
    public function attributes()
33
    {
34
        return [
35 1
            'name' => trans('Name'),
36 1
            'value' => trans('Value'),
37 1
            'type' => trans('Type'),
38 1
            'quantity_per_bundle' => trans('Quantity Per Bundle'),
39 1
            'minimum_order_bundle' => trans('Minimum Order Bundle'),
40 1
            'maximum_order_bundle' => trans('Maximum Order Bundle'),
41 1
            'image' => trans('Image'),
42
        ];
43
    }
44
45
    /**
46
     * Update the image file from the incoming request.
47
     *
48
     * @param  string  $key
49
     * @return string|null
50
     */
51 1
    public function updateImage(string $key = 'image'): ?string
52
    {
53 1
        if (!$this->hasFile($key)) {
54
            return null;
55
        }
56
57
        /** @var \App\Models\Denomination $model */
58 1
        $model = $this->route('denomination');
59
60 1
        if ($model->getRawOriginal('image') && $this->hasFile($key)) {
61 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

61
            Storage::delete(Denomination::IMAGE_PATH . '/' . /** @scrutinizer ignore-type */ $model->getRawOriginal('image'));
Loading history...
62
        }
63
64 1
        $file = $this->file($key);
65
66 1
        $file->storeAs(
67 1
            Denomination::IMAGE_PATH,
68 1
            $filename = ($this->input('value') . '.' . $file->getClientOriginalExtension())
69
        );
70
71 1
        return $filename;
72
    }
73
}
74